Oop In Php(part 2)
A few days ago I started learning how to code and I was like, why not start a blog to also build my skills, well after sharing my first article ever, I was tired and felt horrible, well i don't want to give up, I just want to be a software engineer. now lets continue.
INHERITANCE.
continuing from our previous series, where we talked about class, setters and getters, and objects and properties, let's look at inheritance.
<?php
use User as GlobalUser;
class User {
// the following are properties.
public $username = 'yu';//(what the public means,) = 'ryu';
public $email = 'ryu@thenetninjs.co.uk';
public $role = 'member';
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userone, so we can say;
return "$this->email , belongs to ryu not ru";
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
class AdminUser extends User{
public $level;
public function __construct($username , $name , $level)
{
$this->level = $level;
parent:: __construct($username , $name);
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
$userThree = new AdminUser('tariq' , 'sntpatrick@power.com' , 5);
echo $userThree->username.'<br>';
echo $userThree->email. '<br>';
echo $userThree->level.'<br>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
inheritance is when one class inherits properties and methods from another class using the special keyword extend as shown in the above code. another way of saying this is saying a parent class could have multiple subclasses, the subclasses inherit the properties and methods of the parent class by using the extend keyword.
class AdminUser extends User{
public $level;
public function __construct($username , $name , $level)
{
$this->level = $level;
parent:: __construct($username , $name);
}
}
emphasis on the above block of code, with line,> parent::, this is used because want to add another variable to the constructor function that wasn't in the parent class, in other to do that, we called the construct in the parent class, using that, in other words, we redefined the construct function. let's look at a similar phenomenon, which is overriding classes.
Overriding Properties And Methods: if an extended class wants to override a property and methods, what it needs to do is just to redefine the properties and methods in its extended class itself.
<?php
use User as GlobalUser;
class User {
// the following are properties.
public $username = 'yu';//(what the public means,) = 'ryu';
protected $email = 'ryu@thenetninjs.co.uk';
public $role = 'member';
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userone, so we can say;
return "$this->email , belongs to ryu not ru";
}
public function message(){
return "$this->email just sent a new message";
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
class AdminUser extends User{
public $level;
public $role= 'admin'; overriding the property
private $email;
public function message()
{
return "$this->email is not reachable";
}
public function __construct($username , $email , $level)
{
$this->level = $level;
parent:: __construct($username , $email);
//redefining it after,put it after the parent constructor
// $this->email = $ email;
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
$userThree = new AdminUser('tariq' , 'sntpatrick@power.com' , 5);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
> if you check the above code you see a private access modifier and we knew what that means, even the subclasses cant access the email property, we have another modifier called protected as demonstrated below.
<?php
use User as GlobalUser;
class User {
// the following are properties.
public $username = 'yu';//(what the public means,) = 'ryu';
protected $email = 'ryu@thenetninjs.co.uk';
public $role = 'member';
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userone, so we can say;
return "$this->email , belongs to ryu not ru";
}
public function message(){
return "$this->email just sent a new message";
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
class AdminUser extends User{
public $level;
public $role= 'admin';
// protected $email; //overriding the initial property
public function message()
{
return "$this->email is not reachable";
}
public function __construct($username , $email , $level)
{
$this->level = $level;
parent:: __construct($username , $email);
//redefining it after,put it after the parent constructor
// $this->email = $ email;
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
$userThree = new AdminUser('tariq' , 'sntpatrick@power.com' , 5);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
presenting to you:
CLONE & DESTRUCT
these are magic functions, magic functions are special functions or keywords which come with an underscore. sure we are familiar with the __construct functions, well we have others, which you must research. now let's look at the magic of the destruct function.
__destruct
<?php
use User as GlobalUser;
class User {
// the following are properties.
public $username = 'yu';//(what the public means,) = 'ryu';
protected $email = 'ryu@thenetninjs.co.uk';
public $role = 'member';
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userone, so we can say;
return "$this->email , belongs to ryu not ru";
}
public function message(){
return "$this->email just sent a new message";
}
public function __destruct()
{
echo "$this->username was removed";
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
$userThree = new AdminUser('tariq' , 'sntpatrick@power.com' , 5);
unset($userOne);//(check the result you got) //we use unset for to execute the destruct function.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
__distruct function used to perform any kind of clean up or run any final code, whenever the last reference to an object instance is removed. creating an instance and adding it to the variable, the variable is the reference. using unsetb to remove the reference.
the __clone magic method.
the __clone magic method is used when an object instance is cloned.
<?php
use User as GlobalUser;
class User {
// the following are properties.
public $username = 'yu';//(what the public means,) = 'ryu';
protected $email = 'ryu@thenetninjs.co.uk';
public $role = 'member';
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userone, so we can say;
return "$this->email , belongs to ryu not ru";
}
public function message(){
return "$this->email just sent a new message";
}
// public function __destruct()
// {
// echo "$this->username was removed";
// }
public function __clone()
{
$this->username = $this->username. '(cloned)<br>';
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
class AdminUser extends User{
public $level;
public $role= 'admin';
// protected $email; //overriding the initial property
public function message()
{
return "$this->email is not reachable";
}
public function __construct($username , $email , $level)
{
$this->level = $level;
parent:: __construct($username , $email);
//redefining it after,put it after the parent constructor
// $this->email = $ email;
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
$userThree = new AdminUser('tariq' , 'sntpatrick@power.com' , 5);
$userFour = clone $userOne; // we get an identical copy of the object $userOne in $userFour;
echo $userFour->username;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
please thoroughly observe the code above and every other code.
one last thing, in a situation whereby, we just want to access a property in the class and method directly without using an instance, just accessing the class without creating an instance, we use the static method.
<?php
class weather {
public static $tempConditions = ['cold' ,'mild' , 'warm'];
public static function celsiusToFarenheit($c){
return $c * 9 / 5 + 32;
}
public static function determineTempCondition($f){
if( $f < 40){
return self::$tempConditions[0];
}elseif ($f < 7) {
return self::$tempConditions[1];
}else {
return self::$tempConditions[2];
}
}
}
echo weather::determineTempCondition(5);
// echo weather::celsiusToFarenheit(20);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
my articles are just me trying to express myself in this coding journey, if you ever come across this, please drop reviews.