vijayrami

PHP OOPS

Feb 10th, 2021 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.28 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Define MyClass
  4.  */
  5. class MyClass
  6. {
  7.     public $public = 'Public';
  8.     protected $protected = 'Protected';
  9.     private $private = 'Private';
  10.  
  11.     function printHello()
  12.     {
  13.         echo $this->public;
  14.         echo $this->protected;
  15.         echo $this->private;
  16.     }
  17. }
  18.  
  19. $obj = new MyClass();
  20. echo $obj->public; // Works
  21. echo $obj->protected; // Fatal Error
  22. echo $obj->private; // Fatal Error
  23. $obj->printHello(); // Shows Public, Protected and Private
  24.  
  25.  
  26. /**
  27.  * Define MyClass2
  28.  */
  29. class MyClass2 extends MyClass
  30. {
  31.     // We can redeclare the public and protected properties, but not private
  32.     public $public = 'Public2';
  33.     protected $protected = 'Protected2';
  34.  
  35.     function printHello()
  36.     {
  37.         echo $this->public;
  38.         echo $this->protected;
  39.         echo $this->private;
  40.     }
  41. }
  42.  
  43. $obj2 = new MyClass2();
  44. echo $obj2->public; // Works
  45. echo $obj2->protected; // Fatal Error
  46. echo $obj2->private; // Undefined
  47. $obj2->printHello(); // Shows Public2, Protected2, Undefined
  48.  
  49. ?>
  50. ===================================================================================
  51. <?php
  52. /**
  53.  * Define MyClass
  54.  */
  55. class MyClass
  56. {
  57.     // Declare a public constructor
  58.     public function __construct() { }
  59.  
  60.     // Declare a public method
  61.     public function MyPublic() { }
  62.  
  63.     // Declare a protected method
  64.     protected function MyProtected() { }
  65.  
  66.     // Declare a private method
  67.     private function MyPrivate() { }
  68.  
  69.     // This is public
  70.     function Foo()
  71.     {
  72.         $this->MyPublic();
  73.         $this->MyProtected();
  74.         $this->MyPrivate();
  75.     }
  76. }
  77.  
  78. $myclass = new MyClass;
  79. $myclass->MyPublic(); // Works
  80. $myclass->MyProtected(); // Fatal Error
  81. $myclass->MyPrivate(); // Fatal Error
  82. $myclass->Foo(); // Public, Protected and Private work
  83.  
  84.  
  85. /**
  86.  * Define MyClass2
  87.  */
  88. class MyClass2 extends MyClass
  89. {
  90.     // This is public
  91.     function Foo2()
  92.     {
  93.         $this->MyPublic();
  94.         $this->MyProtected();
  95.         $this->MyPrivate(); // Fatal Error
  96.     }
  97. }
  98.  
  99. $myclass2 = new MyClass2;
  100. $myclass2->MyPublic(); // Works
  101. $myclass2->Foo2(); // Public and Protected work, not Private
  102. ============================================================
  103. <?php
  104. abstract class base {
  105.     public function inherited() {
  106.         $this->overridden();
  107.     }
  108.     private function overridden() {
  109.         echo 'base';
  110.     }
  111. }
  112.  
  113. class child extends base {
  114.     private function overridden() {
  115.         echo 'child';
  116.     }
  117. }
  118.  
  119. $test = new child();
  120. $test->inherited();
  121. ?>
  122. ==>Output will be "base".
  123. ======================================================================================
  124. <?php
  125. abstract class base {
  126.     public function inherited() {
  127.         $this->overridden();
  128.     }
  129.     protected function overridden() {
  130.         echo 'base';
  131.     }
  132. }
  133.  
  134. class child extends base {
  135.     protected function overridden() {
  136.         echo 'child';
  137.     }
  138. }
  139.  
  140. $test = new child();
  141. $test->inherited();
  142. ?>
  143. ===> Output will be "child".
  144. ===============================================================
  145. PHP does support Multi-level inheritance.  (I tested it using version 5.2.9).  It does not support multiple inheritance.
  146.  
  147. This means that you cannot have one class extend 2 other classes (see the extends keyword).  However, you can have one class extend another, which extends another, and so on.
  148. Example:
  149.  
  150. <?php
  151. class A {
  152.         // more code here
  153. }
  154.  
  155. class B extends A {
  156.         // more code here
  157. }
  158.  
  159. class C extends B {
  160.         // more code here
  161. }
  162.  
  163.  
  164. $someObj = new A();  // no problems
  165. $someOtherObj = new B(); // no problems
  166. $lastObj = new C(); // still no problems
  167.  
  168. ?>
  169. <?php
  170.  
  171. class Person
  172. {
  173.     public $name;
  174.     protected $age;
  175.     private $phone;
  176.  
  177.     public function talk(){
  178.         //Do stuff here
  179.     }
  180.  
  181.     protected function walk(){
  182.         //Do stuff here
  183.     }
  184.  
  185.     private function swim(){
  186.         //Do stuff here
  187.     }
  188. }
  189.  
  190. class Tom extends Person
  191. {
  192.     /*Since Tom class extends Person class this means
  193.         that class Tom is a child class and class person is
  194.         the parent class and child class will inherit all public
  195.         and protected members(properties and methods) from
  196.         the parent class*/
  197.  
  198.      /*So class Tom will have these properties and methods*/
  199.  
  200.      //public $name;
  201.      //protected $age;
  202.      //public function talk(){}
  203.      //protected function walk(){}
  204.  
  205.      //but it will not inherit the private members
  206.      //this is all what Object inheritance means
  207. }
  208. =========================================================
  209. The Idea that multiple inheritence is not supported is correct but with tratits this can be reviewed.
  210.  
  211. If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.
  212.  
  213. To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods.
  214.  
  215. <?php
  216. trait A
  217. {
  218.     public function bar()
  219.     {
  220.         echo 'A::bar';
  221.     }
  222. }
  223.  
  224. trait B
  225. {
  226.     public function bar()
  227.     {
  228.         echo 'B::bar';
  229.     }
  230. }
  231.  
  232. trait C
  233. {
  234.     public function bar()
  235.     {
  236.         echo 'C::bar';
  237.     }
  238. }
  239.  
  240. class Foo
  241. {
  242.     use A, B, C {
  243.         C::bar insteadof A, B;
  244.     }
  245. }
  246.  
  247. $foo = new Foo();
  248. $foo->bar(); //C::bar
  249. ====================================
  250. <?php
  251.  
  252. trait A {
  253.     public function sayHello()
  254.     {
  255.         echo 'Hello from A';
  256.     }
  257.  
  258.     public function sayWorld()
  259.     {
  260.         echo 'World from A';
  261.     }
  262. }
  263.  
  264. trait B {
  265.     public function sayHello()
  266.     {
  267.         echo 'Hello from B';
  268.     }
  269.  
  270.     public function sayWorld()
  271.     {
  272.         echo 'World from B';
  273.     }
  274. }
  275.  
  276. class Talker {
  277.     use A, B {
  278.         A::sayHello insteadof B;
  279.         A::sayWorld insteadof B;
  280.         B::sayWorld insteadof A;
  281.     }
  282. }
  283.  
  284. $talker = new Talker();
  285. $talker->sayHello();
  286. $talker->sayWorld();
  287.  
  288. ?>
  289. Hello from A
  290. Fatal error: Uncaught Error: Call to undefined method Talker::sayWorld()
  291. The method sayHello is imported, but the method sayWorld is simply excluded.
  292. ====================================================================
  293. <?php
  294.  
  295. abstract class Cheese
  296. {
  297.       //can ONLY be inherited by another class
  298. }
  299.  
  300. class Cheddar extends Cheese
  301. {
  302. }
  303.  
  304. $dinner = new Cheese; //fatal error
  305. $lunch = new Cheddar; //works!
  306. ==================================================================================
  307. Overloading:
  308. In Overloading entities are processed via magic methods.
  309.  
  310. The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
  311.  
  312. All overloading methods must be defined as public.
  313.  
  314. Property overloading:
  315.  
  316. __set() is run when writing data to inaccessible (protected or private) or non-existing properties.
  317.  
  318. __get() is utilized for reading data from inaccessible (protected or private) or non-existing properties.
  319.  
  320. __isset() is triggered by calling isset() or empty() on inaccessible (protected or private) or non-existing properties.
  321.  
  322. __unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties.
  323.  
  324. Method overloading:
  325.  
  326. __call() is triggered when invoking inaccessible methods in an object context.
  327.  
  328. __callStatic() is triggered when invoking inaccessible methods in a static context.
Add Comment
Please, Sign In to add comment