Definition of the class:
Start with the keyword class followed by the class name, which can be any non-
PHPThe name of the reserved word. Followed by a pair of curly braces containing the definitions of the class members and methods.
Pseudo variable $this
Can be used when a method is in the
Objectused when calling internally. A $this is a reference to the calling object (usually the object to which the method belongs, but it can also be another object if the method is called statically from within the second object).
//?? What is the inside of a static call?
Look at an example:
Class A
{
function foo ()
{
if (Isset ($this)) {
Echo ' $this is defined (';
echo Get_class ($this);
echo ") \ n";
} else {
echo "\ $this is not defined.\n";
}
}
}
Class B
{
function Bar ()
{
A::foo ();
}
}
$a = new A ();
$a->foo ();
A::foo ();
$b = new B ();
$b->bar ();
B::bar ();
The output is:
$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.
/* I would like to know the specific implementation of this example of memory and so on, which I cannot understand for the time being. If anyone can speak clearly, you can tell me. Our dorm bull head is hibernating now, and when he wakes up, I'll ask. */
New
To create an instance of an object, you must create a new object and assign it to a variable. When a new object is created, the object is always assigned, unless the object defines the constructor and throws an exception when an error occurs.
When assigning an instance of an object that has already been created to a new variable, the new variable accesses the same instance and assigns a value to that object. This behavior is the same as when the function is passed into the instance. You can use clones to create a new instance of an object that has already been created.
These words, though short, may not be easy to understand. )
Examples are as follows:
Class Simpleclass
{
Member declaration
Public $var = ' a default value ';
//Method declaration
Public Function Displayvar () {
echo $this->var;
}
}
//See the figure below. 1>new instantiates an object in the heap. 2> points the pointer $instance to him
$instance = new Simpleclass ();
3> pointer $assigned to the instantiated object in the heap
$assigned = $instance;
4> assigns the $instance reference (address) to $reference
$reference =& $instance;
$instance->var = ' $assigned'll has this value ';
$instance = null;//5> Intermittent $instance connection to the instance in the heap.
Var_dump ($instance);
Var_dump ($reference);
Var_dump ($assigned);
Output:
null
Null
Object (Simpleclass) #1 (1) {
["var"]=>
string "$assigned would have the this value"
}
illustrates the entire process:
class inherits extends
A class can inherit methods and members of another class in a declaration with the extends keyword. You cannot extend multiple classes, only one base class can be inherited.
Inherited methods and members can be overwritten by a re-declaration with the same name, unless the parent class defines the method with the final keyword. The overridden method or member can be accessed through the parent::. (What is the mechanism of inheritance?)
The source code is as follows:
Class Extendclass extends Simpleclass
{
Redefine the Parent method
function Displayvar ()
{
echo "Extending class\n";
Parent::d Isplayvar ();
}
}
$extended = new Extendclass ();
$extended->displayvar ();
Output:
Extending class
A default value
http://www.bkjia.com/PHPjc/317411.html www.bkjia.com true http://www.bkjia.com/PHPjc/317411.html techarticle class definition: Start with the keyword class followed by the class name, which can be the name of any non-PHP reserved word. Followed by a pair of curly braces containing the definitions of the class members and methods. ...