Results: 132
class Foo
{
    public static $staticVariable = 'foo';

    public static function getVariableStatic() {
        return self::$staticVariable;
    }

    public function getVariableNONStatic() {
        return self::$staticVariable;
    }
}
We can not access
$staticVariable
using an object directly:
$foo = new Foo();
print $foo->staticVariable;
This will produce the following error message:
Notice:  Accessing static property Foo::$staticVariable as non static...
But there are several ways to access static variable using an object: 1. Using static getter method:
$foo = new Foo();
print $foo->getVariableStatic();
2. Using non-static getter method:
$foo = new Foo();
print $foo->getVariableNONStatic();
php.net:
A property declared as static cannot be accessed with an instantiated class object (though a static method can).
Type hinting means to define which class object we want to receive exactly as a parameter in a constructor
class BLock { }
class Lock {
    private $isLocked;
    
    public function __construct() {
        
    }
    
    public function lock() {
        $this->isLocked = true;
    }
    
    public function unLock() {
        $this->isLocked = false;
    }
    
    public function isLocked() {
        return $this->isLocked;
    }
}
class Chest {
    private $lock;
    
    public function __construct(Lock $lock) {
        $this->lock = $lock;
    }
    
    public function close() {
        $this->lock->lock();
        echo 'Closed' . PHP_EOL;
    }
    
    public function open() {
        if ($this->lock->isLocked()) {
            $this->lock->unLock();
        }
        echo 'Opened' . PHP_EOL;
    }
    
    public function isClosed() {
        return $this->lock->isLocked();
    }
}

$chest = new Chest(new Lock);
// $chest = new Chest(new Block);

$chest->open();
$chest->close();
In this example
Chest
class constructor waits to receive
Lock
class object. If we pass any other class object, we will get an error like the following: Fatal error:
Uncaught TypeError: Argument 1 passed to Chest::__construct() must be an instance of Lock, instance of BLock given
basic terminology of OOP
class
- blueprint, skeleton, basic structure of an object
property
- data of an object, equivalent to PHP variables
method
- behavior of an object, equivalent to PHP functions
inheritance
- ability to use properties and methods of an existing class
polymorphism
- ability to have many forms, when a class has varying functionality while sharing a common interfaces
encapsulation
- to hide or protect certain properties or methods of an object
abstraction
- a concept in which a class has methods without implementation
Assigning new object to a variable causes
__destruct
method to call and the old object gets deleted (the same result when we run
unset($old_object)
)
class User {
    public $username;
    public $friends = ['Tom','David'];

    function __construct($name) {
        $this->username = $name;
        print $this->username . "'s object is created'\n";
    }

    function __destruct() {
        print $this->username . "'s object is deleted'\n";
    }
}
$user1 = new User('Tom');
$user1 = new User('George');
unset($user1);
$user2 = new User('David');

echo "\n\n";
After executing the above code the result will be the following:
Tom's object is created'
George's object is created'
Tom's object is deleted'
George's object is deleted'
David's object is created'


David's object is deleted'
In this example when George's object gets created, Tom's object gets deleted immediately. The reason is that we no longer have Tom's object in
$user1
variable
trait
is kind of solution to the famous
multiple inheritance
problem in PHP. Using traits we can access methods from different traits that we use in our class
class Mobile {
    public function battery() {
        echo 'Battery: MB_06, MF_02, MF_00' . PHP_EOL;
    }
}
trait Laser {
    public function power() {
        echo 'Power: 10 mW' . PHP_EOL;
    }
}
trait Projector {
    public function range() {
        echo 'Range: 2 M' . PHP_EOL;
    }
}
class Galaxy extends Mobile {
    use Laser;
    use Projector;
}

$obj1 = new Galaxy;

$obj1->range();
$obj1->power();
$obj1->battery();
If we use several
traits
in the same class, the traits must not have the same method declared
trait Laser {
    public function who() { 
        echo 'I am a Laser' . PHP_EOL; 
    }
}
trait Projector {
    public function who() { 
        echo 'I am a Laser' . PHP_EOL; 
    }
}
class Galaxy {
    use Laser;//, Projector;
    use Projector;
}
Fatal error:
Trait method who has not been applied, because there are collisions with other trait methods on Galaxy
getmypid
- Gets PHP's process ID
echo 'Current process ID:' . getmypid();
The output will be:
Current process ID:4868
Content of
index.php
file
spl_autoload_register(function($class) {
    // echo 'register class:'.$class."<br>";
    require_once("classes/{$class}.php");
});

// echo 'hey there on line 7'."<br>";

$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;

echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();
Content of
classes/Talkative.php
file
interface Talkative {
    public function talk();
}
Content of
classes/Cat.php
file
class Cat implements Talkative {
    public function talk() {
        return 'Meow' . '<br>';
    }
}
Content of
classes/Dog.php
file
class Dog implements Talkative {
    public function talk() {
        return 'Woof' . '<br>';
    }
}
Content of
classes/Tortoise.php
file
class Tortoise implements Talkative {
    public function talk() {
        return 'Yak yak yak yak ...' . '<br>';
    }
}
Filenames and class names must be THE SAME
Makes our code more modular and reusable
Makes our code easier to maintain
Makes it easier to debug when things go wrong
Makes it possible to hide / protect CLASS properties / methods (encapsulation)
Makes it possible to use other class properties / methods (inheritance)
Makes it possible to use polymorphism
Registers a function that will be executed right before the PHP process stops execution
register_shutdown_function('myexit');

function myexit()
{
	echo "</schedule>";
}
Results: 132