PHP class attributes

Properties (Properties)

Variable members of a class called "Properties", or "field", "features", in this document referred to collectively as "property." Property declarations by keywords  public , protected or  private  at the beginning, then with a normal variable declaration to compose. Attributes can initialize a variable, but the value initialization must be a constant, constant here is the PHP script when compiling the stage can get its value, without depending on the information to be evaluated at runtime.

The relevant  public , protected  and  private  for more detailed information, see the Access Control (visibility) .

Note:

For backward compatibility PHP 4, PHP 5 declared properties can still directly use the keyword  var  instead of (or in addition to) public , protected  , or Private . But no longer need  var  up. In PHP 5.0 to 5.1.3, var  will be considered abandoned and thrown out  E_STRICT warnings, but after 5.1.3 is no longer considered to be abandoned, it will not throw a warning.

If you use direct  var  statement attribute, but did not use  public , protected  or  private  one, PHP 5 will treat it as  public .

In the method of class members which can be used  > - : (Object operator) $ this-> property (where  property  to access non-static property is the property name) in this way. Static property is a  :: (double colon): Self :: $ Property  access. The difference between the static properties and more non-static properties see  Static keyword .

When a method is invoked within the class definition, the dummy variables available  $ the this . $ this  is a reference to the calling object (usually the method is subordinate object, but if it is the second object static when the call might be another object).

 

Property declarations and calls

? < PHP
 class SimpleClass 
{ 
   // an error property declarations 
   public $ var1 = ' Hello ' . ' World ' ;
    public $ var2 = <<< the EOD 
Hello World 
the EOD; 
   public $ = var3 . 1 + 2 ;
    public $ var4 = Self: : myStaticMethod ();
    public $ var5 = $ myVar; 

   // correct property declaration 
   public $ var6 = MyConstant;
    public $ Var7 = Array ( to true , false );

   // After PHP 5.3.0 and, following statement also correct 
   public $ var8 = <<< ' the EOD ' 
Hello World 
the EOD; 
// property called
public getvar1 () {
echo $ this-> var1;
} }

$ = SimpleObject $ SimpleClass new new ();
// call the object properties
echo $ simpleObject-> var1; // outputs "hello world"
 

 

 

Guess you like

Origin www.cnblogs.com/ryanzheng/p/11404814.html