OOP Day 1


Published on

Day one of a two day seminar on object-oriented PHP. Designed to cover a basic level of familiarity with the concepts in general, and some PHP-specific implementation details and examples.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Objects (and I really mean classes here, more on the difference later) are containers. For now you can think of them like associative PHP arrays that can also have functions in them. That will change later but it’s a good starting point.Properties = variables (or array keys => values)Methods = functions
  • Since objects are designed to be self-contained, it’s easier to build software in small pieces, or even spread work out among multiple people (if you’re that lucky)Inheritance lets you write code once and reuse that exact same code elsewhere without duplicating it. More in a later slide.Object-oriented code is built around abstraction and information hiding. If I tell a Mail object to send an email, I don’t care how it does it and I don’t have to know. A user of the system is likely to be represented as an object called User, etc… so the logical flow is easier to verifyIf you name your objects and methods well the code will read more naturallyFinally, lots of OO code has already been written, and since it’s modular, you can just drop it into your app. Examples: Zend Framework, PEAR, etc…
  • Much of the power of OOP comes from the ability to hide complexity and implementation details from the developer. A classic example of this is a database abstraction layer. If you use one of these you can interact with any supported database in the same way, and the DBAL takes care of the differences between systems. It helps free up more brainpower for coding and less for remembering what that variable meant 80 lines ago.Objects should also only be responsible for one thing. If you pack your entire app into one object you’re not really gaining much since you still have to remember everything. Small objects make it easy to find the code you need, when you need it.Because of this segmented development, when I refer to a “user” of an object I usually mean you or another developer, not an end-user of the app
  • Since we only use PHP5 here, the differences shouldn’t matter much unless you’re trying to pass your PHP certification exam or reading old OO code. That being said, a lot of PEAR code is written for PHP 4, so knowing these things might make it easier to read. One of the biggest things is that objects are automatically passed by reference, which makes them much faster. I’ll point out the other differences as we come to them.
  • Classes are containers, like templates for creating objects. You design classes, but you use objects in your code.A class is like a blueprint for a house, whereas an object is the actual house.
  • Classes are the templates used to create objects. You can create as many objects as you want per class. “Bear” is a class, while a particular Bear (like Smokey) is an object. Many people starting out with objects will try to only make one object per class in their code, and that’s not necessary.$this is a special reference to the current object. It’s only valid to use inside an object, you’ll get an error otherwise.
  • You don’t have to define your properties when you write your class, but it’s a very good idea. It’s also a good idea to set a default value for all of your properties, just like if you were defining variables outside of the class. Objects inside of other objects is common.
  • This is where object behavior comes from, i.e. the main thing that make objects different from PHP arrays. You can also redefine behavior in child classes, which I’ll talk about in the next slide.
  • Everything continues to build on these themes, so I want to make sure everyone is clear. I’ll be happy to design an object from scratch for the group if it’d be helpful.
  • This is one of the most powerful features of OOP. Greatly reduces code duplication. “DRY” principle -> Don’t Repeat YourselfSince Bear knows how to sleep(), all child classes of Bear also know how to sleep() for free
  • Some languages allow for multiple inheritance, PHP doesn’t (yet). If you have questions about multiple inheritance, mix-ins, traits, etc… ask me afterwards. Child classes are intended to be more specific than their parents, and if multiple child classes share the same functionality they should be pushed higher up the tree into the parent. It wouldn’t make sense to write PolarBear::eat() and GrizzlyBear::eat() if they’re the same… if they are then eat() should be pushed up into the parent class
  • This ties in directly to inheritance. Visibility is like scope for properties and methods. It determines what can see/affect different parts of the class.Code defensively… if your properties are public you never know what can happen to them. Properties should basically never be public, only (some) methods.Examples after next slide.
  • The underscore naming convention comes from both the PEAR and Zend coding standards, and we highly recommend that you follow them. PEAR says that protected things shouldn’t have an underscore but we don’t follow that here.The newest versions of PHP coding standards (Symfony2, Zend Framework2, etc…) do not add underscores to non-public methods/properties.Old code may not have visibility specified. PHP4 did not have visibility (everything was public).
  • This is where objects start to deviate even further from arrays. Getters and setters (sometimes called accessors and mutators) are class methods that either return or change the values of class properties.If you want a property to be available but read-only from outside the class, you can define just a getter and then the users won’t be able to change it.If you always want a phone number to come back a certain way, you can have your getPhoneNumber() method format the output before returning it to ensure consistencyThis also lets you create “virtual” properties that don’t actually exist in the class, but your users don’t have to know that. An example would be getName() when the class has separate properties for firstName, lastName, and middleInitial. getName could return them in a certain order
  • One of many reasons to use an IDE instead of a text-editor. IDEs are very well-suited to writing OO code and have built-in generators for many common OO methods. They’re not always perfect but they’re usually a good start.A note here: NetBeans 6.9 has a few open bugs on their generators. Most have workarounds, so if you have specific concerns let me know and I’ll tell you how I deal with them.
  • First thing about this slide. We’ll cover magic methods in day 2, but __construct() is by far the most important so we’re covering it early. In PHP4 the class constructor is always a method named the same as the class name. So if I had a PHP4-style constructor for my Bear class, it would be a method called Bear()If you override a constructor in a child class, you should also call parent::__construct() to make sure you don’t miss anythingWhen the constructor finishes, the object should be complete and ready to use. It doesn’t have to contain useful data, but it should only contain valid data, and enough valid data to do its job
  • These aren’t nearly as important. I’m only really covering them here because they make a matched set with __construct(). They also always run automatically, but you don’t usually need to specify any special behavior. One gotcha: if you’re sharing a reference to a database connection between multiple objects and you want to close it, make sure all the other objects are done with it too. Easiest thing is to just not implement these at all, but they’re there if you need ‘em.
  • DateTime is a very useful object that comes built into PHP. Most of the date functions in PHP can also be done via DateTime. DateTime will format a date basically however you want, using the same format strings as the PHP date() function.DateTime::modify() takes all sorts of commands. You can give it a string like “+1 week” and it won’t even flinchsetTimeZone requires a DateTimeZone object, but it’s smart enough to handle daylight savings time for you transparently. It takes 3 letter time zone codes or locations like “America/Chicago”. php.net has a full list
  • This is the end of the second leg of day 1 of our OOP training. Things actually get easier from here on out but I want to make sure everyone is with me so far.
  • If anyone programs in C++, references are like pointers for dummies. For the rest of us, a reference is a way for multiple variables to point to the same thing in the computer’s memory, whether it’s an object, an integer, a string, or whatever. When objects are sent to a function as a parameter, by default, PHP5 will only send a reference to that object instead of copying the whole thing. This lets you (for instance) make one database connection object and send it 5 different places, but still only need one db object instead of 5 copies of the same one. In PHP4 you had to explicitly pass the object by reference to get that efficiency gain, so if you see a lot of ampersands by your parameters the code was probably written for PHP4
  • I like these but I don’t see them used very often. They’re valuable for all the reasons that regular constants are, and I think those are underused too. Constants help avoid “magic” numbers or strings in your code. Just another way of making your code easier to understand.
  • You can also use them to create objects that mimic sets or enums in other languages (structs in C++). An example of an object with just constants might be a Days object with constants for the days of the week instead of numbers (does Sunday start on 0 or 1? Etc…)
  • Type hinting is wonderful and it will make your life easier. It lets you tell PHP what type of input you’re expecting and then PHP will do the work of enforcing it for you. It only works for arrays and objects so far, but there are ways around that.Type hinting also provides information that your IDE can use to help you with code completion.Since it’s enforced by the compiler, it’s really “strict typing” but “hinting” sounds more friendly
  • See you later for Day 2
  • OOP Day 1

    1. 1. Object-Oriented Programming<br />How to melt your brain with PHP<br />Day 1<br />
    2. 2. Intro to OOP Concepts<br />What are objects (classes)?<br />Properties<br />Methods<br />
    3. 3. Advantages of OOP<br />Modular<br />Easy code re-use w/o copy & paste<br />Layers mean less to understand at once<br />Code is more likely to match design<br />More self-documenting than procedural code<br />Free code!<br />
    4. 4. Main Principles<br />Information hiding<br />Abstraction<br />Segmentation<br />Limited responsibility<br />
    5. 5. Main differences in PHP5 (briefly)<br />Objects were completely redone in PHP5<br />All objects are automatically passed by reference<br />Constructors are named differently<br />New visibility options<br />A few other, less important things (RTM)<br />
    6. 6. Classes<br />Defined with class ClassName{…}<br />What is a class?<br />Bear!<br />
    7. 7. Objects<br />Objects are instances of classes<br />Can have many objects per class<br />$this<br />Access properties and methods with “->”<br />With parentheses at the end, it’s a method<br />$this->sleep()<br />No parentheses, it’s a property<br />$this->status<br />Bears!<br />
    8. 8. Object Properties<br />Properties are variables<br />What an object “has” or “knows”<br />Can be any type, even other objects<br />Example Bear properties<br />gender<br />color<br />name<br />weight <br />status<br />
    9. 9. Object Methods<br />Functions<br />What an object can “do”<br />Example Bear methods<br />eat()<br />sleep()<br />
    10. 10. Questions thus far?<br />
    11. 11. Inheritance (pt. 1)<br />Classes inherit properties/methods from their parents<br />Depends on visibility (next topic)<br />Source of the “no copy/paste” code reuse<br />
    12. 12. Inheritance (pt. 2)<br />Uses “extends” keyword<br />PolarBear (child) extends Bear (parent)<br />Only one parent allowed, choose wisely<br />Child classes can redo old stuff or add new things<br />PolarBear can swim()<br />
    13. 13. Visibility (pt. 1)<br />Applies to properties and methods<br />3 choices:<br />public – visible/editable inside and out<br />protected – visible inside class and descendents only<br />private – visible inside original class only<br />Go for private to start, then protected if children need it too<br />
    14. 14. Visibility (pt. 2)<br />Private/protected names often start with an underscore<br />Well-supported convention<br />Applies to properties and methods<br />Being phased out due to IDE support<br />Example: Shy Bear is shy<br />Protecting Bear’s properties/methods<br />PolarBear needs them too<br />
    15. 15. Getters/Setters (pt. 1)<br />How do we use properties that aren’t visible?<br />Advantages<br />More formatting options<br />Clear, explicit interface<br />Allows for read-only properties (getter only)<br />Allows for “virtual” properties<br />
    16. 16. Getters/Setters (pt. 2)<br />Disadvantages<br />More code to write/maintain<br />But… a decent IDE can write it all for you<br />Examples: <br />$bear->getStatus() vs. $bear->status<br />$bear->setName(‘Steve’) vs. $bear->name = ‘Steve’<br />
    17. 17. Constructors<br />A “magic method” that runs when an object is created with the “new” keyword<br />Accepts parameters passed to new<br />$bear = new Bear(‘Steve’);<br />In PHP5 these are named “__construct()”<br />Best practice is to only use these to assign property values<br />IDEs can generate these for you<br />
    18. 18. Destructors<br />Much less commonly-used<br />Runs automatically when the object goes out of scope or is forcibly “unset”<br />Primarily used to close db connections<br />Make sure they’re not still in use, if shared<br />__destruct()<br />
    19. 19. Practical example: DateTime<br />Encapsulates many of the date_* functions<br />DateTime::format()<br />DateTime::modify()<br />DateTime::setTimeZone()<br />Requires a DateTimeZone object<br />
    20. 20. Question breakAnyone?<br />
    21. 21. Exercise (yes, there’s a quiz)<br />Write your own class that extends DateTime<br />Must have the following methods:<br />getMonth()<br />getDayOfWeek()<br />getYear()<br />
    22. 22. Passing objects by reference<br />What references are<br />Automatic in PHP5<br />Using & will actually reverse this and copy the full object<br />Main source of OOP speed boost in PHP5<br />Moral: don’t try to outsmart the compiler<br />
    23. 23. Class constants (pt. 1)<br />Like regular PHP constants, just tied to a class<br />Uses const keyword instead of define()<br />PHP 5.3 can use const outside of classes<br />Can’t be changed in child classes<br />Which is clearer?<br />$result->fetch_array(MYSQL_ASSOC)<br />$result->fetch_array(1)<br />
    24. 24. Class constants (pt. 2)<br />Outside the class: ClassName::CONSTANT<br />Inside: self::CONSTANTor parent::CONSTANT<br />Examples:<br />$bear = new Bear(‘Steve’, Bear::GENDER_MALE);<br />$this->_gender(self::GENDER_MALE);<br />
    25. 25. Type Hinting<br />PHP will enforce a param’s type for you<br />Great feature, use it whenever you can<br />Works for arrays and objects<br />See SplTypeson php.net for more<br />IDEs use type hints for auto-complete<br />Bear example (PoohBear will only eat() Honey)<br />
    26. 26. You made it!<br />Questions? Comments? Death threats?<br />
    27. 27. Resources<br />http://www.php.net/<br />Try php.net/%function_name% for a shortcut<br />http://devzone.zend.com/article/638-PHP-101-part-7-The-Bear-Necessities<br />Source of blatant theft<br />http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/<br />