Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Define MyClass
- */
- class MyClass
- {
- public $public = 'Public';
- protected $protected = 'Protected';
- private $private = 'Private';
- function printHello()
- {
- echo $this->public;
- echo $this->protected;
- echo $this->private;
- }
- }
- $obj = new MyClass();
- echo $obj->public; // Works
- echo $obj->protected; // Fatal Error
- echo $obj->private; // Fatal Error
- $obj->printHello(); // Shows Public, Protected and Private
- /**
- * Define MyClass2
- */
- class MyClass2 extends MyClass
- {
- // We can redeclare the public and protected properties, but not private
- public $public = 'Public2';
- protected $protected = 'Protected2';
- function printHello()
- {
- echo $this->public;
- echo $this->protected;
- echo $this->private;
- }
- }
- $obj2 = new MyClass2();
- echo $obj2->public; // Works
- echo $obj2->protected; // Fatal Error
- echo $obj2->private; // Undefined
- $obj2->printHello(); // Shows Public2, Protected2, Undefined
- ?>
- ===================================================================================
- <?php
- /**
- * Define MyClass
- */
- class MyClass
- {
- // Declare a public constructor
- public function __construct() { }
- // Declare a public method
- public function MyPublic() { }
- // Declare a protected method
- protected function MyProtected() { }
- // Declare a private method
- private function MyPrivate() { }
- // This is public
- function Foo()
- {
- $this->MyPublic();
- $this->MyProtected();
- $this->MyPrivate();
- }
- }
- $myclass = new MyClass;
- $myclass->MyPublic(); // Works
- $myclass->MyProtected(); // Fatal Error
- $myclass->MyPrivate(); // Fatal Error
- $myclass->Foo(); // Public, Protected and Private work
- /**
- * Define MyClass2
- */
- class MyClass2 extends MyClass
- {
- // This is public
- function Foo2()
- {
- $this->MyPublic();
- $this->MyProtected();
- $this->MyPrivate(); // Fatal Error
- }
- }
- $myclass2 = new MyClass2;
- $myclass2->MyPublic(); // Works
- $myclass2->Foo2(); // Public and Protected work, not Private
- ============================================================
- <?php
- abstract class base {
- public function inherited() {
- $this->overridden();
- }
- private function overridden() {
- echo 'base';
- }
- }
- class child extends base {
- private function overridden() {
- echo 'child';
- }
- }
- $test = new child();
- $test->inherited();
- ?>
- ==>Output will be "base".
- ======================================================================================
- <?php
- abstract class base {
- public function inherited() {
- $this->overridden();
- }
- protected function overridden() {
- echo 'base';
- }
- }
- class child extends base {
- protected function overridden() {
- echo 'child';
- }
- }
- $test = new child();
- $test->inherited();
- ?>
- ===> Output will be "child".
- ===============================================================
- PHP does support Multi-level inheritance. (I tested it using version 5.2.9). It does not support multiple inheritance.
- 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.
- Example:
- <?php
- class A {
- // more code here
- }
- class B extends A {
- // more code here
- }
- class C extends B {
- // more code here
- }
- $someObj = new A(); // no problems
- $someOtherObj = new B(); // no problems
- $lastObj = new C(); // still no problems
- ?>
- <?php
- class Person
- {
- public $name;
- protected $age;
- private $phone;
- public function talk(){
- //Do stuff here
- }
- protected function walk(){
- //Do stuff here
- }
- private function swim(){
- //Do stuff here
- }
- }
- class Tom extends Person
- {
- /*Since Tom class extends Person class this means
- that class Tom is a child class and class person is
- the parent class and child class will inherit all public
- and protected members(properties and methods) from
- the parent class*/
- /*So class Tom will have these properties and methods*/
- //public $name;
- //protected $age;
- //public function talk(){}
- //protected function walk(){}
- //but it will not inherit the private members
- //this is all what Object inheritance means
- }
- =========================================================
- The Idea that multiple inheritence is not supported is correct but with tratits this can be reviewed.
- If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.
- 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.
- <?php
- trait A
- {
- public function bar()
- {
- echo 'A::bar';
- }
- }
- trait B
- {
- public function bar()
- {
- echo 'B::bar';
- }
- }
- trait C
- {
- public function bar()
- {
- echo 'C::bar';
- }
- }
- class Foo
- {
- use A, B, C {
- C::bar insteadof A, B;
- }
- }
- $foo = new Foo();
- $foo->bar(); //C::bar
- ====================================
- <?php
- trait A {
- public function sayHello()
- {
- echo 'Hello from A';
- }
- public function sayWorld()
- {
- echo 'World from A';
- }
- }
- trait B {
- public function sayHello()
- {
- echo 'Hello from B';
- }
- public function sayWorld()
- {
- echo 'World from B';
- }
- }
- class Talker {
- use A, B {
- A::sayHello insteadof B;
- A::sayWorld insteadof B;
- B::sayWorld insteadof A;
- }
- }
- $talker = new Talker();
- $talker->sayHello();
- $talker->sayWorld();
- ?>
- Hello from A
- Fatal error: Uncaught Error: Call to undefined method Talker::sayWorld()
- The method sayHello is imported, but the method sayWorld is simply excluded.
- ====================================================================
- <?php
- abstract class Cheese
- {
- //can ONLY be inherited by another class
- }
- class Cheddar extends Cheese
- {
- }
- $dinner = new Cheese; //fatal error
- $lunch = new Cheddar; //works!
- ==================================================================================
- Overloading:
- In Overloading entities are processed via magic methods.
- The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
- All overloading methods must be defined as public.
- Property overloading:
- __set() is run when writing data to inaccessible (protected or private) or non-existing properties.
- __get() is utilized for reading data from inaccessible (protected or private) or non-existing properties.
- __isset() is triggered by calling isset() or empty() on inaccessible (protected or private) or non-existing properties.
- __unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties.
- Method overloading:
- __call() is triggered when invoking inaccessible methods in an object context.
- __callStatic() is triggered when invoking inaccessible methods in a static context.
Add Comment
Please, Sign In to add comment