330 likes | 633 Views
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.
E N D
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
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
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
<?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
<?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
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
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
<?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
SEE “visibility2.php”!!! Property Visibility Example
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
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
Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. Polymorphism
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
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
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
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
<?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