"; } // Method function say() { ech"/> "; } // Method function say() { ech">

Home  >  Article  >  Backend Development  >  Object-oriented features PHP advanced course notes Object-oriented

Object-oriented features PHP advanced course notes Object-oriented

WBOY
WBOYOriginal
2016-07-29 08:40:13876browse

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 . "


";
$user->say();
echo "
";
echo $user G? & Gt;

Example:

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn