×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
constant can not be changed
static variable can not be accessed with the class object
dollar sign is needed to access static variable inside and outside of the class
constant name can not be a keyword, e.g. class (BUT: function, self, parent, static)
Similarities :
both of them are class members
both of them are accessible with "self" keyword inside the class
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);
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
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
interfaceclass 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();
$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_NOTICEprint_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"
}
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()
methodclass 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