Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's new in PHP OOP?

What's new in PHP OOP?

Discusses the new OOP features of PHP 5.3, 5.4, and 5.5

Kristopher Wilson

September 11, 2013
Tweet

More Decks by Kristopher Wilson

Other Decks in Technology

Transcript

  1. PHP < 5.0 ▪ OOP didn't really exist. Sure there

    were classes; that's about it. <?php class MyClass { var $foo; var $poo function MyClass() { // this sucks... } }
  2. PHP 5 ▪ Released: 13 July 2004 ▪ Really real

    OOP! ▪ Cool new things like: Abstract Class, Interface, Public/Private/Protected, Constructors, Destructors! ▪ Subsequent releases: ◦ 5.1: 24 Nov 2005 ◦ 5.2: 02 Nov 2006 ◦ 6.0: LOL
  3. PHP 5.3 ▪ Released: 30 Jun 2009 ▪ End of

    Life: March 2013 ▪ UPGRADE!
  4. PHP 5.3: Namespaces What is a namespace? Named collection of

    "stuff" Classes! Functions! Constants! Variables! Interfaces!
  5. PHP 5.3: Namespaces What is a namespace? Named collection of

    "stuff" Classes! Functions! Constants! Variables! Interfaces! Sounds like some messy code...
  6. PHP 5.3: Namespaces A namespace is a declared, named scoping

    mechanism <?php namespace MyNamespace; // I'm in "MyNamespace"! The namespace keyword, followed by an actual name, declares the namespace of the code that follows.
  7. PHP 5.3: Namespaces A namespace is a declared, named scoping

    mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file... <?php namespace MyNamespace; // I'm in "MyNamespace"!
  8. PHP 5.3: Namespaces A namespace is a declared, named scoping

    mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file... <?php namespace MyNamespace; // I'm in "MyNamespace"!
  9. PHP 5.3: Namespaces Classes are declared in a namespace and

    belong to that namespace. <?php namespace MyNamespace; class MyClass { public function __construct() { // do something! } } The full name of this class is: MyNamespace\MyClass
  10. PHP 5.3: Namespaces <?php $object = new MyClass(); // enjoy

    your FATAL ERROR! $object = new MyNamespace\MyClass(); // enjoy your awesome class! Classes must be accessed using the full namespace path Backslash (\) is the namespace path separator operator thing. Backslash (\) is also the escape character. I hope this feels as awkward for you as it does for me...
  11. PHP 5.3: Namespaces Everything is in a namespace! ▪ Since

    PHP 5.3, every class, function, variable, constant, etc is located within a namespace, even if you don't use them. ▪ If it isn't explicitly in a namespace, it is in the global namespace (or global scope). ▪ The global namespace in PHP is called: \
  12. PHP 5.3: Namespaces When working inside of a namespace, all

    internal PHP classes are located outside of your namespace. To access them, you must precede them by their namespace name (\). <?php namespace Foo; class Bar { public function __construct() { $element = new \SimpleXmlElement('<bar/>'); } }
  13. PHP 5.3: Namespaces <?php use MyNamespace\MyClass; use MyNamespace\OtherClass; $object =

    new MyClass(); $object->setOther(new OtherClass()); The use keyword lets you import a resources from a namespace into your current scope. This helps keep classnames short. A good IDE will generate the use statements for you.
  14. PHP 5.3: Namespaces <?php namespace Foo; use \SimpleXmlElement; class Bar

    { public function __construct() { $element = new SimpleXmlElement('<bar/>'); } } Even classes in the global scope can be imported.
  15. PHP 5.3: Namespaces <?php use MyNamespace\MyClass; use MyOtherNamespace\MyClass as MyOtherClass;

    $object = new MyClass(); $otherObject = new MyOtherClass(); Classes in namespaces can be aliased Useful when two classes in different namespaces have the same name.
  16. PHP 5.3: Namespaces <?php use Zend_Db_Adapter_Pdo_Mysql as Mysql; $db =

    new Mysql(...); Bonus Round! Aliasing is also helpful with really long class names. I'm looking at you Zend Framework 1! * * I know, they couldn't help it. Namespaces weren't around yet...
  17. PHP 5.3: Namespaces <?php namespace MyNamespace\MyPackage; class MyClass { }

    <?php namespace MyNamespace\MyPackage\MyService; class MyOtherClass { } Namespaces can be and should be hierarchical
  18. PHP 5.3: Namespaces In fact... The PSR-0 PHP standard defines

    the way in which you should setup your namespaces. ▪ A namespace must begin with a Vendor Name ▪ The second part should be the name of the package ▪ Further hierarchy is up to the developer, but usually is grouped by component. Example: namespace Doctrine\ORM\Mapping;
  19. PHP 5.3: Namespaces PSR-0 Directory Mapping Further, your namespace hierarchy

    should map 1 for 1 with your directory structure in your source code directory. Namespace: namespace Doctrine\ORM\Mapping; Source Code: /path/to/MyProject/src/Doctrine/ORM/Mapping /Annotation.php /Column.php
  20. PHP 5.3: Namespaces PSR-0 Namespace Standard Following PSR-0 allows for

    autoloader interoperability. This means that, when following PSR-0, resources in your source or library can be autoloaded like any other PSR-0 conforming library. It also makes package managers like Composer work well. Learn more: https://github.com/php-fig
  21. PHP 5.3: Namespaces Why is this so awesome? ▪ Clean

    organization of your code ◦ Aids in separation of concerns by giving "packages" ◦ Allows use of shorter class names ▪ Easily use 3rd party libraries without conflict (aliasing) ▪ Provides a common format for autoloader interoperability (PSR-0)
  22. PHP 5.3: Late Static Binding Previously in PHP, when using

    a static class, you couldn't access an inherited, overridden method or variable.
  23. PHP 5.3: Late Static Binding <?php class SomeClass { public

    static function foo() { return "foo"; } public static function announce() { self::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php foo
  24. PHP 5.3: Late Static Binding <?php class SomeClass { public

    static function foo() { return "foo"; } public static function announce() { self::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php foo that's not really what I wanted...
  25. PHP 5.3: Late Static Binding <?php class SomeClass { public

    static function foo() { return "foo"; } public static function announce() { static::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php bar
  26. PHP 5.3: Late Static Binding <?php class SomeClass { public

    static function foo() { return "foo"; } public static function announce() { static::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php bar So much awesome!
  27. PHP 5.3: Late Static Binding Now in PHP, you can

    totally access an inherited, overridden method or variable. ▪ The self keyword refers to whatever class it is used in, regardless of inheritance. ▪ The static keyword refers to whatever class it is called on, and is evaluated when called, instead of where declared. That being said, limit your use of static classes. They can make testing your code more difficult.
  28. PHP 5.4: Traits ▪ PHP has a single inheritance model.

    You can only inherit from one class. ▪ Sure, you can implement oodles of interfaces, but you still have to rewrite the implementation several times. ▪ This is not DRY. ▪ PHP 5.4 solved this problem by introducing traits.
  29. PHP 5.4: Traits What is a trait? A trait is

    a reusable, defined set of methods and variables that any number of classes can "import" into itself. It's like inheritance, but not quite.
  30. PHP 5.4: Traits Defined like classes <?php trait NamedTrait {

    protected $firstName; protected $lastName; public function setFirstName($firstName) { $this->firstName = $firstName; } public function getFirstName() { return $this->firstName; } public function setLastName($lastName) { $this->lastName = $lastName; } public function getLastName() { return $this->lastName; } }
  31. PHP 5.4: Traits "Imported" into Classes <?php class User {

    use NamedTrait; } class Contact { use NamedTrait; }
  32. PHP 5.4: Traits Use Multiple Traits! <?php class User {

    use DisabledTrait, NamedTrait, HistoryTrait, TimestamptedTrait; } class Contact { use DisabledTrait; use NamedTrait; use TImestamptedTrait; }
  33. PHP 5.4: Traits Now things have stuff! <?php $user =

    (new User()) ->setFirstName('Carl') ->setLastName('Winslow');
  34. PHP 5.4: Traits Benefits ▪ Reusable code ▪ Identical syntax

    (methods, behavior) for things that have the same attributes and behaviors ▪ Multiple inheritance. Things can be more than one thing!
  35. PHP 5.4: Short Array Syntax Long gone are the days

    of using the array() language construct! $users = array('Bob', 'Sue', 'Frank', 'Thomas'); $users = ['Bob', 'Sue', 'Frank', 'Thomas'];
  36. PHP 5.4: Short Array Syntax Long gone are the days

    of using the array() language construct! $users = array('Bob', 'Sue', 'Frank', 'Thomas'); $users = ['Bob', 'Sue', 'Frank', 'Thomas']; This. Is. So. Awesome.
  37. PHP 5.4: Function Array Return Dereferencing Code using functions (or

    methods!) that return arrays can now use the result immediately, without having to store in a variable. <?php function getUser() { return ['lastName' => 'Cabrera', 'firstName' => 'Miguel' } echo getUser()['lastName'] . "\n"; Variables are so 2012.
  38. PHP 5.4: Short Echo Tags ▪ Always on. Always awesome

    <ul> <?php foreach ($this->users as $user): ?> <li> <?= $user->getLastName() ?>, <?= $user->getFirstName() ?><br> <?= $user->getHobbiesInterestsAndThoughtsOnLife() ?> </li> <?php endforeach; ?> </ul>
  39. PHP 5.5 ▪ Released: 18 Jul 2013 ▪ Generators: Return

    early from functions. ▪ try { } catch {} finally {} (finally) ▪ Foreach now supports list() foreach ($array as list($a, $b)) {} ▪ Password Hashing API