Object-oriented features PHP Advanced Course Notes Object oriented

Source: Internet
Author: User
Tags getcolor
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.