1 / 18

PHP OOP

PHP OOP. Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another.

tamma
Download Presentation

PHP OOP

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. PHP OOP

  2. Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality. This is useful for defining and abstracting functionality, and permits the implementation of additional functionality in similar objects without the need to reimplement all of the shared functionality. Object Inheritance

  3. Note: If a class extends another, then the parent class must be declared before the child class structure. This rule applies to class that inherit other classes and interfaces. Object Inheritance

  4. Often you need classes with similar variables and functions to another existing class. In fact, it is good practice to define a generic class which can be used in all your projects and adapt this class for the needs of each of your specific projects. To facilitate this, classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class (this is called 'inheritance' despite the fact that nobody died) and what you add in the extended definition. It is not possible to subtract from a class, that is, to undefine any existing functions or variables. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'. extends

  5. <?php //SAMPLE INHERITANCE class foo{ public function printItem($string){ echo 'Foo:'.$string; } public function printPHP(){ echo 'PHP is great' ; } } class bar extends foo{ public function printItem($string){ echo 'Bar:'.$string; } } $foo = new foo(); $bar = new bar(); $foo->printItem('baz');//Foo: baz $foo->printPHP();//PHP is great $bar->printItem('baz');//Bar: baz $bar->printPHP();//PHP is great ?> Inheritance Example Foo:bazPHP is greatBar:bazPHP is great

  6. <?php //MULTIPLE LEVEL INHERITANCE class A{ function __construct(){ echo "A"; } } class B extends A{ function __construct(){ echo "B"; } } class C extends B{ } class D extends C{ } $objA = new A(); $objB = new B(); $objC = new C(); $objD = new D(); ?> Inheritance Example ABBB

  7. The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member. Visibility

  8. Property Visibility Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public. Property Visibility

  9. <?php class MyClass{ public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello(){ echo $this->public; echo $this->protected; echo $this->private; } } class MyClass2 extends MyClass{ // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function printHello(){ echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); $obj->printHello();//Shows Public, Protected and Private echo $obj->public;//Works //echo $obj->protected;//Fatal Error //echo $obj->private;//Fatal Error echo '<br>'; $obj2 = new MyClass2(); $obj2->printHello();//Shows Public, Protected2, Undefined echo $obj2->public;//Works //echo $obj2->protected;//Fatal Error //echo $obj2->private;//Undefined ?> Property Visibility Example

  10. SEE “visibility2.php”!!! Property Visibility Example

  11. Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. The basis of polymorphism is Inheritance and overridden methods. Polymorphism

  12. class BaseClass { publicfunction myMethod(){ echo"BaseClass method called"; } } class DerivedClass extends BaseClass { publicfunction myMethod(){ echo"DerivedClass method called"; } } function processClass(BaseClass $c){ $c->myMethod(); } $c=new DerivedClass(); processClass($c); Polymorphism Example

  13. Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. Polymorphism

  14. The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way. Polymorphism

  15. A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button “does,” however, depends on what it is connected to and the context in which it is used — but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task. Polymorphism

  16. In the programming world, polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism. Polymorphism

  17. An interface is similar to a class except that it cannot contain code. An interface can define method names and arguments, but not the contents of the methods. Any classes implementing an interface must implement all methods defined by the interface. A class can implement multiple interfaces. Interface

  18. <?php interface employee{ function setdata($empname,$empage); function outputData(); } class Payment implements employee{ function setdata($empname,$empage){ //Functionality } function outputData(){ echo "Inside Payment Class"; } } $a = new Payment(); $a->outputData(); ?> Interface Example

More Related