1 / 39

Object Oriented  Programming with PHP

Object Oriented  Programming with PHP. XU Yinqing , Charles SEEM PHD YEAR 2. Outline(1). What is OOP What is an Object What is a class Commenting code Inheritance (Extending a class ) Visibility (public, private, protected ) Final Abstract Classes Static Methods and properties

marcos
Télécharger la présentation

Object Oriented  Programming with 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 Programming with PHP XU Yinqing, Charles SEEM PHD YEAR 2

  2. Outline(1) • What is OOP • What is an Object • What is a class • Commenting code • Inheritance (Extending a class) • Visibility (public, private, protected) • Final • Abstract Classes • Static Methods and properties • Interfaces

  3. Outline(2) • PHP Class Functions • get_declared_interaces() • get_class() • class_exists() • get_declared_classes() • Autoload • Overloading • Class Constants

  4. What is OOP • There is no hard and fast definition of what Object Oriented Programming (OOP) is • ObjectOriented Programming (OOP) is a programming concept that treats functions and data as objects • This tutorial we will see how data and functions can be represented as re-usable objects

  5. What is an Object • An object is a bunch of variables and functions all lumped into a single entity • The object can then be called rather than calling the variables or functions themselves • Within an object there are methods and properties • The methods are functions that manipulate data within the object. • The properties are variables that hold information about the object.

  6. What is a Class • A class is the blueprint for your object. • The class contains the methods and properties, or the characteristics of the object. • It defines the object. • Use a vehicle object as an example

  7. Vehicle Object Example • All vehicles share similar characteristics, e.g.: number of doors, they are painted some color, they each have a price.  • All vehicles do similar things also, drive, turn left, turn right, stop etc. These can be described as functions, or in OOP parlance, methods. • So, the class holds the definition, and the object holds the value • You declare class in PHP by using the class keyword.

  8. Vehicle Object Example

  9. Vehicle Object Example • With this simple class definition we can now create one, or many, vehicle objects. • To create a new object from the class definition we use the new keyword

  10. Commenting code

  11. Inheritance (Extending a class) • the greatest feature of the PHP OOP model is Inheritance • Inheritance is the ability of php to extend classes (child classes) that inherit the characteristics of the parent class. • The resulting object of an extend class has all the characteristics of the parent class, plus whatever is in the new child, or extended class. • In this instance we will extend the vehicle class and add a motorcycle. A motorcycle is still a vehicle and shares many of the same attributes such as price, drive etc. But a motorcycle has some unique features that a car does not.

  12. Inheritance

  13. Inheritance • You see in the motorcycle class that we have used the keyword extends to extend our vehicle class. • We can then proceed to set vars in the both the parent class and the child class.  • The child class has inherited all the characteristics of the parent vehicle class.

  14. Visibility (public, private, protected) • The visibility of class members, (properties, methods), relates to how that member may be manipulated within, or from outside the class. • Three levels of visibility exist for class members • Public • Private • Protected • Public class members (properties and methods) are available through-out the script and may be accessed from outside the class • By default, all class members are public.

  15. Public

  16. Protected • private methods and properties in a parent class are not visible to child classes and cannot be accessed. • To access a parent method or property from a child class you need to use the protected keyword. • Like the private keyword, protected methods and properties are available only to the class that created them. • But unlike private, protected methods and properties are visible from a parent class.

  17. Private • Having our properties (variables) visible, or accessible from any part of our script can work against us. • A could arise if we lost track of our values and changed the value of $num.

  18. Final • As we saw in the previous section there are ways to protect your code from being used in an improper manner. • Another way of protecting yourself is the Finalkeyword. • Any method or class that is declared as Final cannot be overridden or inherited by another class. ERROR!

  19. Abstract Classes • An abstract class is a class that cannot be instantiated on its own. • You cannot create a new object from it. ERROR!

  20. Abstract class • You can inherit from an abstract class • Any class that extends an abstract parent class must create an interface of the parent abstract methods. If this is not done a fatal error is generated.

  21. Static Methods and Properties • The use of the static keyword allows class members (methods and properties) to be used without needing to instantiate a new instance of the class. • The static declaration must come after the visibility declaration, eg:public static myClass{ • Because there is no object created when using a static call, the keyword $this and the arrow operator, -> are not available. • Static variables belong to the class itself and not to any object of that class. • To access within the class itself you need to use the self keyword along with the :: scope resolution operator.

  22. Static Methods and Properties • The above snippet will echo Bar

  23. Static Methods and Properties • Static properties are often used as counters. Here we will use a basic counter class.

  24. Interfaces • Interfaces in PHP allow you to define a common structure for your classes. • An interface cannot be instantiated on its own.  • One of the goals of OOP is re-use of code. • The interface methods have no internal logic, they are simply a "mapping" or constraint of what the class, or classes, should implement.

  25. PHP Class functions • PHP has available several class functions to help you through the OOP mine field • get_declared_interfaces() • class_exists() • get_class() • get_declared_classes() • Each of these is shown here beginning with the get_declared_interfaces().

  26. get_declared_interfaces() • This helper function provides an array of all the available declared interfaces.

  27. Other functions $foo is from the fax class 1 0 -> stdClass 1 -> Exception 2->… 3->… … 106 -> fax 107 -> printer

  28. Autoload • The class definition must be included in every call to an object • This is commonly achieved with the include() or require() functions such as below

  29. Autoload • The solution to this sort of mess is __autoload() • The __autoload() function will internally search out the class and load its definition.

  30. Overloading • Overloading in PHP has caused much confusion for no real reason • PHP Overloading can be broken down into two basic components • Method overloading • Property overloading • Method Overloading is achieved by a special function named __call() • It is available as a sort of method wildcard for calls to undefined methods within a class. This special function is only called when the original method name does not exist. • The __call() will only work when the class method you are trying to access does not exist

  31. Overloading:_call() • Of course the above snippet of code will produce an error such asFatal error: Call to undefined method my_class::bar() in /www/overload.php on line 12. because we have called the bar() class method that does not exist. • Enter __call(). With the __call() function in place, PHP will try to create the function and you have any code within the _call() method that you like.  • The __call() method takes two arguments, the method name, and the arguments. • Your call to the undefined method may have many arguments and these are returned in an array.

  32. Overloading:_call() • The above code will print the followingbarArray ( [0] => arg1 [1] => arg2 ) • The __call() method has returned the method name that we called along with the array of args passed to it.

  33. Overloading • The second part of overloading refers to properties and the ability to be able to dynamically get and set object properties. • The __get() function is called when reading the value of an undefined property, and __set() is called when trying to change that properties value.

  34. Overloading:_set() • The result from above will be:The value of bar is Blue Smarties • We have described a class named candy which contains a public property named $type •  It has a simple method and our __set() method. After the class our user code creates a new instance of the candy class. • Then we try to set a variable that does not exist in the class. Here the __set method takes control and assigns it for us. We then see in our __set method that it echoes the name of the variable, plus its intended value. The __set() method takes two arguments, the name of the non-existantvariable, and its intended value.

  35. Overloading:_get() • From the above code we get the resultRetrieving element of $choctype property with index of milkThe value of the following element property is 0

  36. Class Constants • You have more than likely seen the use standard constants in PHP. To define a standard constant we use this code:

  37. Class Constants • To define a class constant we use the const keyword.

  38. Class Constants • Each of the above methods would output the same lineAn Error has occurred!

  39. End • Thank you!

More Related