PHP OOP: Class, Method, Object, Instance, Declare, Echo, Inheritance,Constructor,Access Control,Static!

PHP OOP example described here from edwin’s video:

<?php

class Car{
    var $wheels=4;
    var $hood=1;
    var $engine=1;
    var $doors=4;
    
    function MoveWheels(){
        //echo "Wheels move";
        //$this means reference inside the class for outside we need to create instance of a class with object and call by reference with variable or method to show;
        $this->wheels=4;
        
    }
    
    
    function CreateDoors(){
        $this->doors=6;
    }
}

$bmw=new Car(); //instance of the class
$truck=new Car(); //new instance for truck

$bmw->MoveWheels();
//$bmw->wheels=8;
echo $bmw->wheels."</br>";
echo $truck->wheels=10;
echo "<br>";
$truck->CreateDoors();
echo $truck->doors;

//if(class_exists("car", "MoveWheels")){
//    echo "Method Exists";
//}else{
//    echo "No";
//}
?>

Inheritance example:

<?php
class Car{

var $wheel=14;
var $hood=1;
var $engine=1;
var $doors=4;

function MoveWheeels(){
$this->wheels=14;
}

function CreateDoors(){
$this->doors=6;
}
}

class Plane extends Car{
 var $wheels=20;
}
$jet=new Plane();
echo $jet->wheels;
?>

Constructor:

<?php

class Car{
    var $wheels=4;
    var $hood=1;
    var $engine=1;
    var $doors=4;
    
    function __construct(){
        //echo "Wheels move";
        //$this means reference inside the class
       echo $this->wheels=4;
        
    }
    
    
    function CreateDoors(){
        echo $this->doors=6;
        echo "</br>";
    }
}


$truck=new Car();
echo "</br>";
echo $truck->CreateDoors();

?>

Access control: Public, Private, Protected

<?php

class Car{
    public $wheels=4;
    protected $hood=1;
    private $engine=1;
    var $doors=4;
    
    function showProperty(){
        //echo "Wheels move";
        //$this means reference inside the class
        echo $this->engine;       
    }
}

$bmw=new Car(); //instance of the class
$semi=new Semi();
Class Semi extends Car{
    
    
}
echo $bmw->showProperty();
//echo $bmw->showProperty();
?>

Static Data in Class:

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *