First, class and object
Not only in PHP, but in all object-oriented programming languages, classes are abstract, and objects are an instance of a class. All abstractions are: "Extract the Resemblance".
In life, a person is a class, pull alike is: People have facial features, hair, hands and feet, will eat sleep and so on.
Take a look at the implementation of class and object code in PHP
<?php class Person{ //定义一个叫Person的类 public $name; //声明共有的属性:名字 public function say(){ //声明共有的方法:说话 echo ‘saying‘; } }?>
Constructors and destructors
A constructor is a method that executes when an object is new, and the destructor is the method that executes when the object is destroyed.
The destruction of the object can be destroyed either explicitly or automatically after the code page finishes executing.
<?php class person { //Define a class called person Public $name;//Declaration of common attributes: First name Public function say(){ //Declaration of common methods: speaking Echo ' saying '; } Public function __construct(){ //fixed __construct declaration constructor Echo ' construct ';//Output at object creation time} Public function __destruct(){ Echo ' Destruct ';//object is destroyed when output}}$a=NewPerson ();//Output construct //To output destruct after code ends?>
The binding of this
ThIsreturnbackwhenbeforetiedfixedof thetheElephant. in theP H P inasFruitto bein theSquareMethodBodyinTunewiththeElephantinof theGenusSex,Allto bePluson This is otherwise considered to be a local variable.
<?php class person { //Define a class called person Public $name=' Color ';//Declaration of common attributes: First name Public function say(){ //Declaration of common methods: speaking Echo ' saying '; } Public function __construct(){ Echo $this->name;//Output color, if echo $name will error, because $name is undefined at this time}}$a=NewPerson ();?>
Author: by: Rodan Http://blog.csdn.net/sunyuan_software
PHP classes and objects, constructors and destructors, plus this binding