vijayrami

More OOPS

Feb 10th, 2021 (edited)
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.94 KB | None | 0 0
  1. Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
  2.  
  3. <?php
  4. class Foo {
  5.     public static function aStaticMethod() {
  6.         // ...
  7.     }
  8. }
  9.  
  10. Foo::aStaticMethod();
  11. $classname = 'Foo';
  12. $classname::aStaticMethod();
  13. ?>
  14. ======================================================
  15. <?php
  16. class Foo
  17. {
  18.     public static $my_static = 'foo';
  19.  
  20.     public function staticValue() {
  21.         return self::$my_static;
  22.     }
  23. }
  24.  
  25. class Bar extends Foo
  26. {
  27.     public function fooStatic() {
  28.         return parent::$my_static;
  29.     }
  30. }
  31.  
  32.  
  33. print Foo::$my_static . "\n";
  34.  
  35. $foo = new Foo();
  36. print $foo->staticValue() . "\n";
  37. print $foo->my_static . "\n";      // Undefined "Property" my_static
  38.  
  39. print $foo::$my_static . "\n";
  40. $classname = 'Foo';
  41. print $classname::$my_static . "\n";
  42.  
  43. print Bar::$my_static . "\n";
  44. $bar = new Bar();
  45. print $bar->fooStatic() . "\n";
  46. ?>
  47. =============Outout of the above================
  48. foo
  49. foo
  50.  
  51. Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23
  52.  
  53. Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23
  54.  
  55. foo
  56. foo
  57. foo
  58. foo
  59.  
  60. PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation.
  61.  
  62. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.
  63.  
  64. <?php
  65. abstract class AbstractClass
  66. {
  67.     // Force Extending class to define this method
  68.     abstract protected function getValue();
  69.     abstract protected function prefixValue($prefix);
  70.  
  71.     // Common method
  72.     public function printOut() {
  73.         print $this->getValue() . "\n";
  74.     }
  75. }
  76.  
  77. class ConcreteClass1 extends AbstractClass
  78. {
  79.     protected function getValue() {
  80.         return "ConcreteClass1";
  81.     }
  82.  
  83.     public function prefixValue($prefix) {
  84.         return "{$prefix}ConcreteClass1";
  85.     }
  86. }
  87.  
  88. class ConcreteClass2 extends AbstractClass
  89. {
  90.     public function getValue() {
  91.         return "ConcreteClass2";
  92.     }
  93.  
  94.     public function prefixValue($prefix) {
  95.         return "{$prefix}ConcreteClass2";
  96.     }
  97. }
  98.  
  99. $class1 = new ConcreteClass1;
  100. $class1->printOut();
  101. echo $class1->prefixValue('FOO_') ."\n";
  102.  
  103. $class2 = new ConcreteClass2;
  104. $class2->printOut();
  105. echo $class2->prefixValue('FOO_') ."\n";
  106. ?>
  107. The above example will output:
  108.  
  109. ConcreteClass1
  110. FOO_ConcreteClass1
  111. ConcreteClass2
  112. FOO_ConcreteClass2
  113. ================================================================
  114. Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented.
  115.  
  116. Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined.
  117.  
  118. All methods declared in an interface must be public; this is the nature of an interface.
  119.  
  120. To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
  121. ============================================================
  122. <?php
  123. class Base {
  124.     public function sayHello() {
  125.         echo 'Hello ';
  126.     }
  127. }
  128.  
  129. trait SayWorld {
  130.     public function sayHello() {
  131.         parent::sayHello();
  132.         echo 'World!';
  133.     }
  134. }
  135.  
  136. class MyHelloWorld extends Base {
  137.     use SayWorld;
  138. }
  139.  
  140. $o = new MyHelloWorld();
  141. $o->sayHello();
  142. ?>
  143. The above example will output:
  144.  
  145. Hello World!
  146. ========================================================
  147. <?php
  148. trait HelloWorld {
  149.     public function sayHello() {
  150.         echo 'Hello World!';
  151.     }
  152. }
  153.  
  154. class TheWorldIsNotEnough {
  155.     use HelloWorld;
  156.     public function sayHello() {
  157.         echo 'Hello Universe!';
  158.     }
  159. }
  160.  
  161. $o = new TheWorldIsNotEnough();
  162. $o->sayHello();
  163. ?>
  164. The above example will output:
  165.  
  166. Hello Universe!
  167. ===========================================================================
  168. Final Keyword ΒΆ
  169. The final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
  170.  
  171. Example #1 Final methods example
  172.  
  173. <?php
  174. class BaseClass {
  175.    public function test() {
  176.        echo "BaseClass::test() called\n";
  177.    }
  178.    
  179.    final public function moreTesting() {
  180.        echo "BaseClass::moreTesting() called\n";
  181.    }
  182. }
  183.  
  184. class ChildClass extends BaseClass {
  185.    public function moreTesting() {
  186.        echo "ChildClass::moreTesting() called\n";
  187.    }
  188. }
  189. // Results in Fatal error: Cannot override final method BaseClass::moreTesting()
  190. ?>
  191. Example #2 Final class example
  192.  
  193. <?php
  194. final class BaseClass {
  195.    public function test() {
  196.        echo "BaseClass::test() called\n";
  197.    }
  198.  
  199.    // Here it doesn't matter if you specify the function as final or not
  200.    final public function moreTesting() {
  201.        echo "BaseClass::moreTesting() called\n";
  202.    }
  203. }
  204.  
  205. class ChildClass extends BaseClass {
  206. }
  207. // Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
  208. ?>
  209.  
Add Comment
Please, Sign In to add comment