Example 1:
Copy code The code is as follows:
// Class definition
class User
{
// Attributes, pay attention to the scope of public, private, protected
public $name = "hackbaby";
// Constructor
function __construct()
{
echo "construct
";
}
// Method
function say()
{
echo "This is called in the class itself: $this ->name";
}
// Destructor
function __destruct()
{
echo "destruct";
}
// Returns the current object The description information is called through the instantiated variable name. For example, $user in this example
function __toString()
{
return "user class";
}
}
//instance If the constructor has parameters, use $user = new User('parameter');
$user = new User();
echo $user->name . "
" ;
$user->say();
echo "
";
echo $user;
?>
Example 2 :
Copy code The code is 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('extra large');
echo $apple->getsize();
?>
http://www.bkjia.com/PHPjc/320157.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320157.htmlTechArticleExample 1: Copy the code as follows: ?php // Class definition class User { // Attributes, pay attention to public , private, protected scope public $name = "hackbaby"; // Constructor functio...