Exploring the Static keyword of oo in PHP and the detailed _php of class constants

Source: Internet
Author: User
Tags modifiers
Free to find a little bit of PHP about OO point data to see, access control modifiers, self,parent,const,static a few keywords, the arrow operator (the book is also called. That is, "and"), the scope of the resolution operator (that is, double colons "::"), but think this with the C # language oo A lot is the same, it is easy to understand, but still see how to clear the PHP oo ideas or should be.
--------------------------------------------------------------------------------
Declare the members and methods of a static class so that it does not require an instance of a class. The declaration of a static member cannot be accessed through an instance of a class object (although a static method can).
A static declaration must be after a visibility declaration. To be compatible with PHP 4, if no visibility is declared, then the members and methods will be treated as if they have been declared public.
Because static methods can invoke non-object instances, pseudo-variable $this cannot be used in methods declared as static.
In fact, the static method invocation form is determined at compile time. When you use a class name that must be declared, the method is an application that is fully identified and has no inheritance rules. This method is fully validated when using the class name that must be declared, and no inherited rules are used.
If self is already declared, then self is interpreted by the class to which it is currently owned. Also does not apply to inheritance rules. A static property cannot be-&gt by an arrow operator; accesses a non-static method, which results in a e_strict-level warning.
Copy CodeThe code is as follows:
Class 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 = new Foo ();
Print $foo->staticvalue (). " /n ";
Print $foo->my_static. " /n ";//Undefined" property "My_static
$foo:: My_static is not possible
Print Bar:: $my _static. " /n ";
$bar = new Bar ();
Print $bar->foostatic (). " /n ";
?>

Copy CodeThe code is as follows:
Static methods instance (static Method Example)
Class Foo
{public static function Astaticmethod () {}
}
Foo::astaticmethod ();
?>

Constants can be defined in each base class so that it remains the same. When you do not use the $ symbol to declare or use it, the constant is different from the normal variable. Just like a static member, a constant value cannot be accessed through an instance of an object (instead of using $object::constant). A constant value must be a constant expression, not a variable, a member of a class, a mathematical expression, or a result of a function call.
Copy CodeThe code is as follows:
Class MyClass
{Const CONSTANT = ' constant value ';
function Showconstant () {echo self::constant. /n "; }
}
echo myclass::constant. " /n ";
$class = new MyClass ();
$class->showconstant ();//echo $class:: constant; is not allowed
?>

http://www.bkjia.com/PHPjc/327503.html www.bkjia.com true http://www.bkjia.com/PHPjc/327503.html techarticle free to find a little bit of PHP about OO point data to see, access control modifiers, self,parent,const,static a few keywords, the arrow operator (the book is also called. i.e. "-"), ...

  • Related Article

    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.