Basic php knowledge: Class and object (1)

Source: Internet
Author: User
Basic php knowledge: Class and object (1) class definition:
It starts with a keyword class and follows the class name. it can be any non- PHPThe name of the reserved word. Followed by a pair of curly braces, the bread contains class member and method definitions.

Pseudo variable $ this
When a method ObjectUsed for internal calls. $ This is a reference to the calling object (usually the object of the method, but it can also be another object, if the method is called statically from the second object.
//?? What is the internal static call?
Let's look at an example:
Class
{
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-> foo ();
A: foo ();
$ B = new B ();
$ B-> bar ();
B: bar ();
Output result:
$ This is defined ()
$ This is not defined.
$ This is defined (B)
$ This is not defined.
/* I want to know the specific implementation of memory in this example, which I cannot understand for the moment. If anyone can make it clear, let me know. Our dormitory is in hibernation now. when he wakes up, I will ask. */

New
To create an instance of an object, you must create a new object and assign it to a variable. This object is always assigned a value when a new object is created, unless the object defines the constructor and throws an exception when an error occurs.
When you assign an instance created by an object to a new variable, the new variable accesses the same instance, which is the same as the value assigned by this object. This behavior is the same as when the function is passed into the instance. You can create a new instance by cloning it to a created object.
(These statements are short but not easy to understand .)
Example:
Class SimpleClass
{
// Member declaration
Public $ var = 'A default value ';

// Method declaration
Public function displayVar (){
Echo $ this-> var;
}
}
// See the figure below. 1> new instantiate an object in heap (heap. 2> direct the pointer $ instance to him
$ Instance = new SimpleClass ();
// 3> point the pointer $ assigned to the instantiated object in heap.
$ Assigned = $ instance;
// 4> assign the reference (address) of $ instance to $ reference
$ Reference = & $ instance;
$ Instance-> var = '$ assigned will have this value ';
$ Instance = null; // 5> interrupted $ instance connection to the heap instance.
Var_dump ($ instance );
Var_dump ($ reference );
Var_dump ($ assigned );
Output:
NULL
NULL
Object (SimpleClass) #1 (1 ){
["Var"] =>
String (30) "$ assigned will have this value"
}
The entire process is illustrated as follows:


Class inheritance extends
A class can inherit the methods and members of another class with the extends keyword in the declaration. You cannot expand multiple classes and can inherit only one base class.

The inherited methods and members can be overwritten by re-declaring with the same name, unless the final keyword is used when the parent class defines the method. You can use parent: To access covered methods or members. (What is the inheritance mechanism ?)
The source code is as follows:
Class ExtendClass extends SimpleClass
{
// Redefine the parent method
Function displayVar ()
{
Echo "Extending class \ n ";
Parent: displayVar ();
}
}
$ Extended = new ExtendClass ();
$ Extended-> displayVar ();
Output:
Extending class
A default value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.