PHP Advanced Course Notes Object Oriented_PHP Tutorial

WBOY
Release: 2016-07-21 15:46:16
Original
979 people have browsed it

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();

?>

www.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!