Results: 133
Magic method
__construct
gets called when a new object of the class gets created
class User {
    public $username;
    public $friends = ['Tom','David'];

    function __construct($name) {
        $this->username = $name;
        // print "In BaseClass constructor\n";
    }
    
    public function addFriend($friend_name) {
        $this->friends[] = $friend_name;
    }
}
$user1 = new User('George');
echo $user1->username;
Returns a list of methods of the class being passed to the function
class User {
    public $username = 'George';
    public $friends = ['Tom','David'];
    
    public function addFriend($friend_name) {
        $this->friends[] = $friend_name;
    }
}
$user1 = new User();

print_r( get_class_methods('User') );
Returns an array with properties of the class
class User {
    public $username;
    public $friends;
    
    public function addFriend($friend_name) {
        $this->friends[] = $friend_name;
    }
}
$user1 = new User();
print_r( get_class_vars('User') );
Returns the name of the class that is used to create the object passed to the
get_class
function
class User {
    public $username;
    public $friends;
    
    public function addFriend($friend_name) {
        $this->friends[] = $friend_name;
    }
}
$user1 = new User();
echo get_class($user1);
Properties (attributes):
color
,
length
,
top speed
Methods (behavior):
start
,
stop
,
accelerate
signal
PDO duplicate entry error code
We get the error code (using DB unique constraint) when the resource is already created
try{
    $stmt = $conn->prepare($query);
    $stmt->execute($exec_arr);
} catch(PDOException $e) {
    if($e->getCode() == 23000){
        array_push($errors, 'Technology already exists');
    } else {
        array_push($errors, 'Database error');
    }
}
$res = preg_replace('/\\\\u([a-f0-9]{4})/e', "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($this->response));
Modern way of doing this is:
$res = json_encode($this->response, JSON_UNESCAPED_UNICODE);
JSON_UNESCAPED_UNICODE
added in 5.4 PHP version
include resource
The statement does not work when config.php is one step out of the root directory on servage.net
include_once '../../config.php';
But works one of these lines:
include_once './../config.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/../config.php';
Output of the second solution is:
/storage/content/95/1009995/test.sibrdzne.ge/public_html/../config.php
Late static binding with attribute CODE
class a {
	const OPERATOR_ID = 0;
	public function test(){
		echo self::OPERATOR_ID;
		echo static::OPERATOR_ID;
	}
}
class b extends a {
	const OPERATOR_ID = 1;
}
(new b())->test();
Array KEY outside of LOOP
$key
is useful outside of the
foreach
loop
$array = ['key1'=>1234, 'key2'=>2345, 'key3'=>3457];
foreach ($array as $key => $item) {
	
}
echo $key; // key3
Results: 133