__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__destruct
gets called when we delete an object manually.
If we don't delete the object during the code execution, it will be called automatically at the end of the code executionclass 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('George');
$user2 = new User('David');
echo $user1->username . "\n";
__construct
gets called when a new object of the class gets createdclass 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;
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') );
class User {
public $username;
public $friends;
public function addFriend($friend_name) {
$this->friends[] = $friend_name;
}
}
$user1 = new User();
print_r( get_class_vars('User') );
get_class
functionclass User {
public $username;
public $friends;
public function addFriend($friend_name) {
$this->friends[] = $friend_name;
}
}
$user1 = new User();
echo get_class($user1);
name
, gender
, username
, email
Methods (behavior): addFriend
, deleteFriend
, postStatus
, follow
, unfollow
class User {
public $username;
public $friends;
public function addFriend($friend_name) {
$this->friends[] = $friend_name;
}
public function removeFriend($friend_name) {
$this->friends = array_filter($this->friends, fn ($m) => $m != $friend_name);
}
}
$user1 = new User();
$user1->username = 'Adam';
$user1->addFriend('Tom');
$user1->addFriend('David');
$user1->addFriend('George');
$user1->removeFriend('David');
print_r($user1->friends);
echo $user1->username;
color
, length
, top speed
Methods (behavior): start
, stop
, accelerate
signal
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 implementationMakes 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