"; } // Method function say() { ech"/> "; } // Method function say() { ech">
Home > Article > Backend Development > Object-oriented features PHP advanced course notes Object-oriented
Example 1:
Copy the code The code is as follows:
// Class definition
class User
{
// Attributes, pay attention to the scope of public, private and protected
public $name = "hackbaby";
// Constructor
function __construct()
{
echo "construct
";
}
// Method
function say()
{
echo "This is in the class itself Call: $this->name";
}
// Destructor
function __destruct()
{
echo "destruct";
}
// Return the description information of the current object. Call it through the instantiated variable name. For example $user in this example
function __toString()
{
return "user class";
}
}
//Instantiation, if the constructor has parameters, use $user = new User('parameter');
$ user = new User();
echo $user->name . "
Copy code code is as follows:
& lt;? PHP
Class FRUIT {
Protected $ FrUit_Color; $ 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('extra large');
echo $apple->getsize();
?>
The above introduces the object-oriented characteristics. PHP Advanced Course Notes Object-oriented, including the content of object-oriented characteristics. I hope it will be helpful to friends who are interested in PHP tutorials.