Example one:
Copy the Code code as follows:
Definition of Class
Class User
{
attribute, note the scope of public, private, protected
Public $name = "Hackbaby";
constructor function
function __construct ()
{
echo "Construct
";
}
Method
function Say ()
{
echo "This is called in the class itself: $this->name";
}
Destructors
function __destruct ()
{
echo "Destruct";
}
Returns the description of the current object called by the instantiated variable name, for example, $user in this example
function __tostring ()
{
Return "User Class";
}
}
instantiated, if the constructor has parameters then use $user = new user (' parameter ');
$user = new User ();
Echo $user->name. "
";
$user->say ();
echo " ";
Echo $user;
?>
Example two:
Copy the Code code as follows:
Class Fruit
{
protected $fruit _color;
protected $fruit _size;
function SetColor ($color)
{
$this->fruit_color = $color;
}
function GetColor ()
{
return $this->fruit_color;
}
function SetSize ($size)
{
$this->fruit_size = $size;
}
function GetSize ()
{
return $this->fruit_size;
}
function Save ()
{
Code
}
}
Class Apple extends Fruit
{
Private $variety;
function Setvariety ($type)
{
$this->variety = $type;
}
function Getvariety ()
{
return $this->variety;
}
}
$apple = new Apple ();
echo $apple->setvariety (' Red Fuji ');
echo $apple->getvariety ();
echo "
";
echo $apple->setcolor (' Red ');
echo $apple->getcolor ();
echo "
";
echo $apple->setsize (' Mega ');
echo $apple->getsize ();
?>
The above describes the object-oriented features of PHP Advanced Course Notes Object-oriented, including the object-oriented features of the content, I hope to be interested in PHP tutorial friends helpful.