in PHP5 • All objects are automatically passed by reference • Constructors are named differently • New visibility options • A few other, less important things (RTM)
many objects per class • $this! • Access properties and methods with “->” – With parentheses at the end, it’s a method • $this->sleep()! – No parentheses, it’s a property • $this->status! • Bears!
3 choices: – public – visible/editable inside and out – protected – visible inside class and descendents only – private – visible inside original class only • Go for private to start, then protected if children need it too
underscore – Well-supported convention – Applies to properties and methods – Being phased out due to IDE support • Example: Shy Bear is shy – Protecting Bear’s properties/methods – PolarBear needs them too
is created with the “new” keyword • Accepts parameters passed to new! – $bear = new Bear(‘Steve’);! • In PHP5 these are named “__construct()” • Best practice is to only use these to assign property values – IDEs can generate these for you
object goes out of scope or is forcibly “unset” • Primarily used to close db connections – Make sure they’re not still in use, if shared • __destruct()!
in PHP5 – Using & will actually reverse this and copy the full object – Main source of OOP speed boost in PHP5 • Moral: don’t try to outsmart the compiler
tied to a class – Uses const keyword instead of define()! – PHP 5.3 can use const outside of classes • Can’t be changed in child classes • Which is clearer? – $result->fetch_array(MYSQL_ASSOC)! – $result->fetch_array(1)!
you • Great feature, use it whenever you can • Works for arrays and objects – See SplTypes on php.net for more • IDEs use type hints for auto-complete • Bear example (PoohBear will only eat() Honey)