Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
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
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
Magic method
__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 execution
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('George');
$user2 = new User('David');
echo $user1->username . "\n";
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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;
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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') );
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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') );
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
Properties (attributes):
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;
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
2
Properties (attributes):
color
,
length
,
top speed
Methods (behavior):
start
,
stop
,
accelerate
signal
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
2
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
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
Results: 1578