Front-End Learning PHP Object-oriented series fourth-----key words

Source: Internet
Author: User

Public

Public, which has the greatest access, is defined as a public class member that can be accessed from anywhere

If the attribute is defined with Var, it is considered public and the method defaults to public if the method does not have a keyword set.

<?phpclass demo{public    $public = 1;    function test ($var) {        echo "{$var}000";    }} $d 1 = new demo; $d 1->test ($d 1->public);//1000?>

Protected

Protected represents a protected, class member that is defined as protected and can be accessed by its subclasses and parent classes

<?phpclass demo{    protected function fn () {        echo ' 111 ';    }} Class Demo1 extends demo{    function test () {        parent::fn ();    }    } $d 1 = new Demo1; $d 1->test ();//111?>

Private

Private means that members of a class defined as private can only be accessed by the class in which they are defined

<?phpclass demo{    Private $private = 1;    function test () {        echo ($this->private);    }} $d 1 = new demo; $d 1->test ();//1?>    

Final

PHP5 added the final keyword, which can only be used to decorate classes and methods , not to use the final keyword to decorate member properties , because final is the meaning of constants, we define constants in PHP using the The define () function and the const keyword , so you cannot use final to define member properties

If a method in the parent class is declared final, the child class cannot overwrite the method . If a class is declared final, it cannot be inherited

<?phpclass baseclass {public   function test () {       echo "baseclass::test () called\n";   }   Final public Function moretesting () {       echo "baseclass::moretesting () called\n";   }} Class ChildClass extends BaseClass {public   function moretesting () {       echo "childclass::moretesting () called\n ";   }} Results in Fatal error:cannot override final Method baseclass::moretesting ()?>

Static

The static keyword is meant to be statically used to decorate the member properties and member methods of a class (that is, static properties and static methods)

static properties and static methods in a class can be accessed directly using the class name without instantiating (new)

[note] A static property cannot be accessed through an object that is instantiated by a class, but a static method can

Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method, and a static property cannot be accessed by an object through the operator.

Invoking a non-static method in a static manner results in an e_strict level error

Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used . You can initialize a static property to an integer or an array, but not to another variable or function return value, or to an object

<?phpclass foo{public    static $my _static = ' Foo ';    Public Function Staticvalue () {        return self:: $my _static;    }} Class Bar extends foo{public    function foostatic () {        return Parent:: $my _static;    }} Print Foo:: $my _static. "\ n";//' foo ' $foo = new foo ();p rint $foo->staticvalue (). "\ n";//' foo ' Print $foo:: $my _static. "\ n";//' foo ' Print $foo->my_static. "\ n"; Error?>

Const

You can define a value that is always the same in a class as a constant. you do not need to use the $ symbol when you define and use constants, but instead use the const

The value of a constant must be a fixed value, cannot be a variable, a class property, a result of a mathematical operation, or a function call

<?phpclass myclass{    Const constant = ' constant value ';    function Showconstant () {        echo  self::constant. "\ n";    }} Echo Myclass::constant. "\ n";//' constant value ' $classname = "MyClass"; Echo $classname:: Constant. "\ n"; ' constant value ' $class = new MyClass (); $class->showconstant ();//' constant value ' echo $class:: Constant. " \ n ";//' constant value '?>

This

When a method is called inside a class, there is an available pseudo-variable, this, which is a reference to the member method inside the object, which represents one of the references to this object, but can only be used in the object's member methods, whether within the object using the This accesses the internal members of its own object. Or to access members of an object through the object's reference name outside the object, you need to use the special operator, "-" to complete the access

[Note that]this is not available in static methods

<?phpclass a{    function foo ()    {        if (isset ($this)) {            echo ' $this is defined (';            echo Get_class ($this);            echo ") \ n";        } else {            echo "\ $this is defined.\n";}        }    } Class b{    function bar ()    {        ///Note:the Next line would issue a warning if e_strict is enabled.        A::foo ();    }} $a = new A (), $a->foo ();//$this is defined (a) a::foo ();//$this is not defined. $b = new B (), $b->bar ();//$this is defined (b) b::bar ();//$this is not defined.? >

Self

In a method of a class, you cannot refer to a static variable or static method with this, and you need to use self to refer to the

<?phpclass myclass{    Const constant = ' constant value ';    static function Showconstant () {        echo  self::constant. "\ n";    }} $var = new Myclass;echo $var->showconstant ();//constant value?>

Parent

Parent is used to call member methods or constants defined in the parent class in a subclass

<?phpclass myclass{    function fn () {        echo (' 111 ');    }    Const A = ' a ';} Class Class1 extends myclass{    function test () {        echo parent::fn (). parent::a;    }} $var = new Class1; $var->test ();//111a?>

Front-End Learning PHP Object-oriented series fourth-----key words

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.