1 / 48

Object-Oriented PHP

Object-Oriented PHP. Developing Object-Oriented PHP. Topics: OOP concepts – overview, throughout the chapter Defining and using objects Defining and instantiating classes Defining and using variables, constants, and operations Getters and setters

jstyles
Download Presentation

Object-Oriented PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented PHP

  2. Developing Object-Oriented PHP • Topics: • OOP concepts – overview, throughout the chapter • Defining and using objects • Defining and instantiating classes • Defining and using variables, constants, and operations • Getters and setters • Defining and using inheritance and polymorphism • Building subclasses and overriding operations • Using interfaces • Advanced object-oriented functionality in PHP • Comparing objects, Printing objects, • Type hinting, Cloning objects, • Overloading methods,

  3. Object-Oriented Programming • Object-oriented programming (OOP) refers to the creation of reusable software object-types / classes that can be efficiently developed and easily incorporated into multiple programs. • In OOP an object represents an entity in the real world (a student, a desk, a button, a file, a text input area, a loan, a web page, a shopping cart). • An OOP program = a collection of objects that interact to solve a task / problem.

  4. Object-Oriented Programming • Objects are self-contained, with data and operations that pertain to them assembled into a single entity. • In procedural programming data and operations are separate → this methodology requires sending data to methods! • Objects have: • Identity; ex: 2 “OK” buttons, same attributes → separate handle vars • State → a set of attributes (aka member variables, properties, data fields) = properties or variables that relate to / describe the object, with their current values. • Behavior → a set of operations (aka methods) = actions or functions that the object can perform to modify itself – its state, or perform for some external effect / result.

  5. Object-Oriented Programming • Encapsulation (aka data hiding) central in OOP • = access to data within an object is available only via the object’s operations (= known as the interface of the object) • = internal aspects of objects are hidden, wrapped as a birthday present is wrapped by colorful paper  • Advantages: • objects can be used as black-boxes, if their interface is known; • implementation of an interface can be changed without a cascading effect to other parts of the project → if the interface doesn’t change

  6. Object-Oriented Programming • Classes are constructs that define objects of the same type. A class is a template or blueprint that defines what an object’s data and methods will be. Objects of a class have: • Same operations, behaving the same way • Same attributes representing the same features, but values of those attributes (= state) can vary from object to object • An object is an instance of a class. (terms objects and instances are used interchangeably) • Any number of instances of a class can be created.

  7. OOP in Web Programming • Small Web projects • Consist of web scripts designed and written using an ad-hoc approach; a function-oriented, procedural methodology • Large Web software projects • Need a properly thought-out development methodology – OOP → • OO approach can help manage project complexity, increase code reusability, reduce costs. • OO analysis and design process = decide what object types, what hidden data/operations and wrapper operations for each object type • UML – as tool in OO design, to allow to describe classes and class relationships

  8. Creating Classes in PHP • A minimal class definition: class classname { // classname is a PHP identifier! // the class body = data & function member definitions } • Attributes • are declared as variables within the class definition using keywords that match their visibility: public, private, or protected. (Recall that PHP doesn't otherwise have declarations of variables → data member declarations against the nature of PHP?) • Operations • are created by declaring functions within the class definition.

  9. Creating Classes in PHP • Constructor = function used to create an object of the class • Declared as a function with a special name: function __construct (param_list) { … } • Usually performs initialization tasks: e.g. sets attributes to appropriate starting values • Called automatically when an object is created • A default no-argument constructor is provided by the compiler only if a constructor function is not explicitly declared in the class • Cannot be overloaded (= constructors for a class); if you need a variable # of parameters, use flexible parameter lists…

  10. Creating Classes in PHP • Destructor = opposite of constructor • Declared as a function with a special name, cannot take parameters function __destruct () { … } • Allows some functionality that will be automatically executed just before an object is destroyed • An object is removed when there is no reference variable/handle left to it • Usually during the "script shutdown phase", which is typically right before the execution of the PHP script finishes • A default destructor provided by the compiler only if a destructor function is not explicitly declared in the class

  11. Instantiating Classes • Create an object of a class = a particular individual that is a member of the class by using the newkeyword: $newClassVariable=new ClassName(actual_param_list); • Notes: • Scope for PHP classes is global (program script level), as it is for functions • Class names are case insensitive as are functions • PHP 5 allows you to define multiple classes in a single program script • The PHP parser reads classes into memory immediately after functions  class construction does not fail because a class is not previously defined in the program scope.

  12. Using Data/Method Members • From operations within the class, class’s data / methods can be accessed / called by using: • $this = a variable that refers to the current instance of the class, and can be used only in the definition of the class, including the constructor & destructor • The pointer operator ->(similar to Java’s object member access operator “.” ) • class Test { public $attribute; function f ($val){ $this -> attribute = $val; // $this is mandatory! } // if omitted, $attribute is treated }// as a local var in the function No $ sign here

  13. Using Data/Method Members • From outside the class, accessible (as determined by access modifiers) data and methods are accessed through a variable holding an instance of the class, by using the same pointer operator. class Test { public $attribute; } $t = new Test(); $t->attribute = “value”; echo $t->attribute;

  14. BankAccount.php • http://www.nku.edu/~frank/csc301/Examples/PHP_OO/BankAccount.php • http://www.nku.edu/~frank/csc301/Examples/PHP_OO/BankAccount_php.pdf

  15. Defining and Using Variables, Constants and Functions • Three access / visibility modifiers introduced in PHP 5, which affect the scope of access to class variables and functions: • public : public class variables and functions can be accessed from inside and outside the class • protected : hides a variable or function from direct external class access + protected members are available in subclasses • private : hides a variable or function from direct external class access • An access modifier has to be provided for each class instance variable • Static class variables and functions can be declared without an access modifier → default is public

  16. Getters and Setters • Encapsulation : hide attributes from direct access from outside a class and provide controlled access through accessor and mutator functions • You can write custom getVariable() / setVariable($var) functions or • Overload the functionality with the __get() and __set() functions in PHP • __get() and __set() • Prototype: mixed __get($var); // param represents the name of an attribute, __get returns the value of that attribute void __set($var, $value); // params are the name of an attribute and the value to set it to

  17. Getters and Setters • __get() and __set() • Can only be used for non-static attributes! • You do not directly call these functions; For an instance $acc of the BankAccount class: $acc->Balance = 1000; implicitly calls the __set() function with the value of $name set to ‘Balance’, and the value of $value set to 1000. (__get() works in a similar way)

  18. Getters and Setters • __get() and __set() functions’ value: a single access point to an attribute ensures complete control over: • attribute’s values function __set($name, $value) { echo "<p>Setter for $name called!</p>"; if (strcasecmp($name, "Balance")==0 && ($value>=0)) $this->$name = $value; ... } • underlying implementation: as a variable, retrieved from a db when needed, a value inferred based on the values of other attributes

  19. Multiple Source Files • Recommended:Create one PHP source file per class definition. • This aids class reuse and script clarity.

  20. Remember these PHP Constructs? • require(….) • Includes file specified, terminateson errors • include(…) • Includes file specified, gives warningon errors • require_once(….) • Includes file specified only if it has not already been included, terminateson errors • include_once(….) • Includes file specified only if it has not already been included, gives warningon errors  Really useful but would require you to write a long list of include() or require() statements at the beginning of each script, one for each class. In PHP5, this is no longer necessary. You may define an __autoload function! Example16-6.php

  21. function __autoload() • The function is invokedautomatically each time a class is required but has not been defined. • We can insert this function into our script: function __autoload($class_name) { require_once$class_name . '.php'; } Note: Class_name = File_name

  22. function __autoload() • Usually you define an autoloader in one file and include this one on every request. If you use bootstrapping (meaning: a single file, that catches every request and redirects it to the appropriate target) its only required to define the autoloader there. So its not required to define it in every file.

  23. function __autoload() <?php function __autoload($class_name) { require_once $class_name . '.php'; } $objSimon = newperson; $objSimon->setDisplayFirstnameSurname(“Napoleon", “Reyes"); $objBike = newvehicle("Bicycle"); echo "<p>Vehicle: " . $objBike->getDescription() . "</p>"; ?> Class definition comes from another file.

  24. autoload vs. include vs require • Autoloader is used for lazy initialization. It's the most effective with MVC architecture, not with websites that include a file here and there and define db connection string in every file (which is terrible). • Using autoload with MVC framework saves resources and brings a lot to modularity. Since you don't have to include files with classes, you just instantiate a class you require in the controller you're currently at. • Basically, it's an OOP thing. You shouldn't worry about it if you're not using object approach to structuring your website and include/require is what will work for you.

  25. spl_autoload_register vs. __autoload • _autoload is generally considered obsolete. It only allows for a single autoloader. Generally, you should only use __autoload if you're using a version of PHP without support for spl_autload_register. • spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a match is found. • Note : the __autoload() function can also be used for autoloading classes and interfaces, its preferred to use the spl_autoload_register() function. This is because it is a more flexible alternative (enabling for any number of autoloaders to be specified in the application, such as third party libraries). For this reason, using __autoload() is discouraged and it may be deprecated in the future.

  26. More on classes • There are methods for “introspection” about classes, i.e. the ability of a program to examine an object’s characteristics. For example, the function class_exists() can be used (surprise!) to determine whether a class exists or not. The function get_declared_classes() returns an array of declared classes. $classes = get_declared_classes(); You can also get an array of method names in any of the following (equivalent) manners: $methods = get_class_methods(Person); $methods = get_class_methods('Person'); $class = 'Person'; $methods = get_class_methods($class);

  27. More introspection functions • There are a wide variety of introspection functions, several more are listed below. get_class_vars($object); /* gets an associative array that maps property names to values (including inherited properties), but it *only* gets properties that have default values (those initialized with simple constants) */ is_object($object); // returns a boolean value get_class($object); /* returns the class to which an object belongs */ method_exists($object, $method); // returns a boolean value get_object_vars($object); /* returns an associative array mapping properties to values (for those values that are set (i.e. not null) */

  28. A word about object methods… • When defining the names of your own object methods, you should generally avoid starting them with a double underscore. Why? • There are some “built-in” or predefined PHP method names that start in this manner, most notably constructors and destructors using the __construct() and __destructor() names. In the future (in new versions of PHP), it’s possible that further methods might be defined that begin with a double underscore. • __get() and __set(), these methods give an opportunity to either retrieve a value or set the (default?) value for that property. For example, if a class is used to represent data obtained from a database, you could write __get() and __set() methods that read and write data whenever requested.

  29. Inheritance • There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code.

  30. Other Example

  31. Class product : • name • price • discount • Class CDMusic : • artist • Genre • Class CDRack • capacity • model

  32. So, why should we use interfaces? • Quite simply, interfaces make our code less brittle. If you doubt this statement, ask anyone who's been forced to maintain legacy code that wasn't written against interfaces. • The interface is a contract between the programmer and his/her code. The interface says, "As long as you play by my rules you can implement me however you like and I promise I won't break your other code." • So as an example, consider a real-world scenario (no cars or widgets):

  33. Traits • A trait is similar to an abstract class which cannot be instantiated on its own (though more often it’s compared to an interface). The PHP documentation defines traits as follows: • Traits is a mechanism for code reuse in single inheritance languages such as PHP. • A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

  34. Let’s consider this example: • <?php class DbReader extends Mysqli { } class FileReader extends SplFileObject { }

  35. It’d be a problem if both classes needed some common functionality, for example making both of them singletons. • Since PHP doesn’t support multiple inheritance, either each class will have to implement the necessary code to support the Singleton pattern or there will be an inheritance hierarchy that doesn’t make sense. • Traits offer a solution to exactly this type of problem.

  36. trait Singleton { private static $instance; public static function getInstance() { if (!(self::$instance instanceof self)) { self::$instance = new self; } return self::$instance; } }

  37. class DbReader extends ArrayObject { use Singleton; } class FileReader { use Singleton; }

  38. The trait Singleton has a straight forward implementation of the Singleton pattern with a static method getInstance() which creates an object of the class using this trait (if it’s not already created) and returns it. <?php $a = DbReader::getInstance(); $b = FileReader::getInstance(); var_dump($a); //object(DbReader) var_dump($b); //object(FileReader)

  39. We can see that $a is an object of DbReader and $b is an object of FileReader, but both are now behaving as singletons. The method from Singleton has been horizontally injected to the classes using it. • Traits do not impose any additional semantics on the class. In a way, you can think of it as a compiler-assisted copy and paste mechanism where the methods of the trait is copied into the composing class. • If we were simply subclassing DbReader from a parent with a private $instance property, the property wouldn’t be shown in the dump of ReflectionClass::export(). And yet with traits, there it is!

  40. Multiple Traits <?php trait Hello { function sayHello() { echo "Hello"; } } trait World { function sayWorld() { echo "World"; } } class MyWorld { use Hello, World; } $world = new MyWorld(); echo $world->sayHello() . " " . $world->sayWorld(); //Hello World

  41. Conflict Resolution and Aliasing trait Game { function play() { echo "Playing a game"; } } trait Music { function play() { echo "Playing music"; } } class Player { use Game, Music; } $player = new Player(); $player->play();

  42. Conflict Resolution and Aliasing Such trait conflicts aren’t resolved automatically for you. Instead, you must choose which method should be used inside the composing class using the keyword insteadof. <?php class Player { use Game, Music { Music::play insteadof Game; } } $player = new Player(); $player->play(); //Playing music

  43. Conflict Resolution and Aliasing but still avoiding conflicts. It’s possible to introduce a new name for a method in a trait as an alias. An alias doesn’t rename the method, but offers an alternate name by which it can be invoked. Aliases are created using the keyword as. <?php class Player { use Game, Music { Game::play as gamePlay; Music::play insteadof Game; } } $player = new Player(); $player->play(); //Playing music $player->gamePlay(); //Playing a game

  44. Trait : other feature We know that in classical inheritance the private properties of a class can’t be accessed by child classes. Traits can access the private properties or methods of the composing classes, and vice versa! Here is an example: <?php trait Message { function alert() { echo $this->message; } } class Messenger { use Message; private $message = "This is a message"; } $messenger = new Messenger; $messenger->alert(); //This is a message

  45. Trait : other feature Here we have a trait Message with an abstract method define(). It requires all classes which use this trait to implement the method. Otherwise, PHP will give an error saying there is an abstract method which has not been implemented. <?php trait Message { private $message; function alert() { $this->define(); echo $this->message; } abstract function define(); } class Messenger { use Message; function define() { $this->message = "Custom Message"; } } $messenger = new Messenger; $messenger->alert(); //Custom Message

  46. Trait : Summary • Traits are one of the most powerful features introduced in PHP 5.4, and I’ve discussed almost all their features. • They let programmers reuse code fragments horizontally across multiple classes which do not have to be within the same inheritance hierarchy. • Instead of having complex semantics, they provide us with a light weight mechanism for code reuse.

More Related