Results: 132
Examples of using class constant
class Goodbye {
  const LEAVING_MESSAGE = "Thank you for visiting US!\n";
  public function byebye() {
    echo self::LEAVING_MESSAGE;
  }
}

$goodbye = new Goodbye();

// Access const variable with getter method
$goodbye->byebye();

// Access const variable with class name
echo Goodbye::LEAVING_MESSAGE;

// Access const variable directly with object
echo $goodbye::LEAVING_MESSAGE;
About class constants:
Constants cannot be changed once it is declared
Class constants are case-sensitive
A class constant is declared inside a class with the const keyword
It is recommended to name the constants in all uppercase letters
__destruct
method will be executed at the end (before the script stops execution)
class Bill {
    public $dinner = 20;
    public $dessert = 5;
    public $drink = 3;
    
    public $bill = 0;
    public function __construct() {
        $this->bill = 10;
    }
    public function dinner($count) {
        $this->bill += $count * $this->dinner;
        return $this;
    }
    public function dessert($count) {
        $this->bill += $count * $this->dessert;
        return $this;
    }
    public function drink($count) {
        $this->bill += $count * $this->drink;
        return $this;
    }
    public function __destruct() {
        echo $this->bill;
    }
}

$bill = new Bill;

$bill->dinner(3)->dessert(2)->drink(1);
Serializes the object to a value that can be serialized natively by
json_encode()
class MyClass implements JsonSerializable {
    public function jsonSerialize() {
        return json_encode([1, 2, "44"]);
    }
}
$c = new MyClass();
echo json_encode($c);
In this example after calling
json_encode($c)
function,
JsonSerializable
will be called automatically because the class
MyClass
implements
JsonSerializable
Classes implementing Countable can be used with the count() function
class MyClass implements Countable {
    public function count() {
        return 5;
    }
}
$c = new MyClass();
echo $c->count();
echo count($c); //calls $c->count();
We can use count() function for the class instance, when the class implements
Countable
interface
Dependency injection is a technique when we pass one class object to another class constructor:
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) {
        $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->open();
$chest->close();
Triggers error if
$divisor
equals to 0
$divisor = false;
if ($divisor == 0) {
    trigger_error("Cannot divide by zero", E_USER_WARNING);
}
Possible second parameter values:
E_USER_NOTICE
E_USER_WARNING
E_USER_ERROR
The second parameter defaults to E_USER_NOTICE
Fetches all HTTP headers from the current request
print_r(json_encode(getallheaders()));
The result of the above code is:
{
    "Content-Type": "application/json",
    "User-Agent": "PostmanRuntime/7.26.8",
    "Accept": "*/*",
    "Postman-Token": "a89b677d-776a-45d7-988c-f18ad2512522",
    "Host": "localhost",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Content-Length": "137"
}
The built-in function
http_response_code
Sets HTTP response status code
404
http_response_code(404);
Late Static Binding
gives us ability to call child class method from parent class. In this example
who()
method of child class will be called from parent class
test()
method
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Here comes Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
Another example of late static binding: parent class uses child class property -
tableName
class Model
{
	protected static $tableName = 'Model';

	public static function getTableName()
	{
		return static::$tableName;
	}
}

class User extends Model
{
	protected static $tableName = 'User';
}

echo User::getTableName(); // User
Polymorphism is - when multiple classes have different functionality but they all share common interface
interface Element {
    public function characteristics();
}
class Water implements Element {
    public function characteristics() {
        return [
            'Water characteristic 1',
            'Water characteristic 2',
            'Water characteristic 3',    
        ];
    }
}
class Fire implements Element {
    public function characteristics() {
        return [
            'Fire characteristic 1',
            'Fire characteristic 2',
            'Fire characteristic 3',    
        ];
    }
}
class Air implements Element {
    public function characteristics() {
        return [
            'Air characteristic 1',
            'Air characteristic 2',
            'Air characteristic 3',    
        ];
    }
}
class Earth implements Element {
    public function characteristics() {
        return [
            'Earth characteristic 1',
            'Earth characteristic 2',
            'Earth characteristic 3',    
        ];
    }
}


function describe(Element $element) {
    echo get_class($element) . "\n";
    $characteristics = $element->characteristics();
    if (is_array($characteristics)) {
        foreach ($characteristics as $characteristic) {
            echo $characteristic . "\n";
        }
        echo "\n\n";
    }
}

$element = new Water;
describe($element);

$element = new Fire;
describe($element);

$element = new Air;
describe($element);

$element = new Earth;
describe($element);
Results: 132