Walk into php-classes and objects

Source: Internet
Author: User

PHP use class to define the class, with new instantiation of the object, with extends inheriting the class, but only single inheritance, properties and methods have public, private and protected do access control, the default is public, in the class defined constants do not need to \$, using:: A range resolver can call a method of a parent class to access static variables, static methods, and constants of a class.

The definition of each class begins 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. The pseudo variable \ $this can be used when a method is called inside an object. \ $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).

1<?PHP2 classA3 {4   functionfoo ()5   {6     if(isset($this)){7       Echo' $this is defined (';8       Echo Get_class($this);9       Echo") \ n";Ten}Else{Echo' $this is not defined ';}  One   } A } - classB - { the   functionBar () -   { -A::foo (); -   } + } - $a=NewA (); + $a-foo (); AA::foo (); at $b=NewB (); - $b-bar (); -B::bar (); -?>
View Code

=================================================

A simple class definition:

<? PHP class simpleclass{    //  member declaration public    $var = ' A default value ';     // Method declaration     Public function Displayvar () {        echo$this-var;    }}? >
View Code

=================================================

Inherited:

A class can inherit the methods and members of another class in a declaration with the extends keyword. PHP does not support multiple inheritance, and a class can inherit only one base class.

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::.

<? PHP class extends simpleclass{    //  Redefine The parent method    function  displayvar ()    {        echo "Extending class\n";        Parent::Displayvar ();    }} $extended New Extendclass (); $extended-Displayvar ();? >
View Code

==================================================

In the member method of the class, you can access the properties and methods of the class through \ $this->property (property is the attribute name), but to access the static properties of the class or to use it in a static method, use Self:: $property.

===================================================

Class constants:

The value of a constant must be a fixed value and cannot be the result of a variable, class property, or other operation, such as a function call.

<?PHPclassmyclass{Const constant= ' constant value '; functionshowconstant () {EchoSelf::constant. "\ n"; }}EchoMyClass::constant. "\ n";$classname= "MyClass";Echo $classname::constant. "\ n";//after PHP 5.3.0$class=NewMyClass ();$class-showconstant ();Echo $class::constant." \ n ";//after PHP 5.3.0?>
View Code

===================================================

constructor function:

If a constructor is defined in a subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the child class.

Destructors:

Walk into php-classes and objects

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.