PHP OOP object is the instance of the class. Objects can be created from the class through a new
keyword. We can create more than one object from the class. The process of creating objects from the class is known as instantiation.
new
keyword is used.->
operator.
Source Code
<?php
class House{
public $houseColor;
public $houseModel = "ASD202"; //property
public function geetMessage(){ //method
echo"This house model is really best.";
}
}
$myHouseObject = new House;
//creating object
$myFriendHouseObject = new House;
//creating object
?>
Here, two objects namely $myHouseObject and $myFriendHouseObject are created using the class House.
Class property and method can be accessed by ->
operator.
Source Code
<?php
class House{
public $houseColor;
public $houseModel = "ASD202"; //property
public function geetMessage(){ //method
echo"This house model is really best.";
}
}
$myHouseObject = new House;//creating object
$myFriendHouseObject = new House; //creating object
$myHouseObject->houseModel;
$myHouseObject->geetMessage();
?>
Please keep in mind that, do not put $
sing before the property and method name during accessing the property and method of an object.