Basics of PHP: Classes and Objects (1) _php tips

Source: Internet
Author: User
Tags inheritance

The definition of the class:
starts with the keyword class, followed by the class name, and can be any non- PHP reserved word name. Followed by a pair of curly braces, which contain the definition of class members and methods.

Pseudo variable $this
can be used when a method is invoked inside an object . $this is a reference to the calling object (usually the object to which the method belongs, but can also be another object, if the method is statically invoked from within the second object).
//?? What exactly is 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 results are:
$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 memory and so on, I am temporarily unable to understand. If someone can speak clearly, you can tell me. Our dormitory cow head is hibernating now, waiting for him to wake up, I will ask. */

New
to create an instance of an object, you must create a new object and assign it to a variable. The object is always assigned when the new object is created, unless the object defines the constructor and throws an exception when an error occurs.
When you assign an instance of an object that has already been created to a new variable, the new variable accesses the same instance and assigns the same value as the object. This behavior is the same as when passing a function into an instance. You can use cloning to create a new instance of an object that has already been created.
These few words, though very short, may not be easily understood. )
Examples are as follows:
class Simpleclass
{
Member declaration
Public $var = ' a default value ';

//method declaration
Public Function Displayvar () {
Echo $this->var;
}
}
Look at the figure below. 1>new instantiate an object in the heap (heap). 2> pointed the pointer $instance to him.
$instance = new Simpleclass ();
3> pointer $assigned point to an instantiated object in heap
$assigned = $instance;
4> assigns the $instance reference (address) to $reference
$reference =& $instance;
$instance->var = ' $assigned'll have this value ';
$instance = null;5> Intermittent $instance connection to instances in heap.
Var_dump ($instance);
Var_dump ($reference);
Var_dump ($assigned);
Output:
NULL
Null
Object (Simpleclass) #1 (1) {
["Var"]=>
String "$assigned'll have this value"
}
Diagram the entire process:


class Inheritance extends
A class can inherit methods and members of another class in a declaration using the extends keyword. You cannot extend multiple classes, only one base class can be inherited.

inherited methods and members can be overridden by the same name, unless the parent class defines the method using the final keyword. You can access the overridden method or member through 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

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.