Explore the static keywords of oo in PHP and the detailed _php techniques of class constants

Source: Internet
Author: User
Tags inheritance php and static class

Free to find a little bit of PHP about OO Point of information to see, access control modifiers, self,parent,const,static several keywords, the arrow operator (also called in the book). That is, "->"), the range parsing operator (that is, double colon "::"), but feel that this is the same as the C # language oo Many are the same, it is easy to understand but still look at the understanding of the OO in PHP or should be.
--------------------------------------------------------------------------------
Declare the members and methods of a static class so that it does not require an instance of a class. A static member's declaration cannot be accessed through an instance of a class object (although a static method can).
The static declaration must be after the visibility declaration. In order to be compatible with PHP 4, if no visibility is declared, then members and methods will be treated as if they have been declared public.
Because static methods can call non-object instances, pseudo variable $this cannot be used in methods declared 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 of full identity and no inheritance rules. When you use the name of a class that must be declared, this method is fully validated and no inherited rules are used.
If self is already declared, then self is interpreted by the class that currently belongs to. Also does not apply to inheritance rules. A static property cannot access a Non-static method by using the arrow operator, which produces a e_strict level warning.

Copy Code code as follows:

<?php
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 Code code as follows:

Static methods instance (static Method Example)
<?php
Class Foo
{public static function Astaticmethod () {}
}
Foo::astaticmethod ();
?>

You can define constants in each base class to keep it unchanged. Constants differ from ordinary variables when you do not use the $ symbol to declare or use it. Like static members, constant values 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 the result of a function call.
Copy Code code as follows:

<?php
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
?>

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.