Open the PHP Static keyword Tour mode _php instance

Source: Internet
Author: User

Declaring a class member or method static, you can access it directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.

For compatibility with PHP4, if "visibility" is not specified, properties and methods are public by default.

The pseudo variable $this is not available in static methods because the static method does not need to be invoked through the object.

Static properties cannot be accessed by an object through the-> operator.

Using:: Calling a non-static method can result in a e_strict level error.

Like all other PHP static variables, static properties can only be initialized to a character value or a constant, and expressions cannot be used. So you can initialize a static property to an integer or an array, but you can't point to another variable or function return value, or point to an object.

After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.

Example #1 static Member code example

<?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. " ";
$foo = new Foo ();
Print $foo->staticvalue (). " ";
Print $foo->my_static. " ";   Undefined "Property" my_static 
print $foo:: $my _static. " ";
$classname = ' Foo ';
Print $classname:: $my _static. " "; You can dynamically call print Bar after PHP 5.3.0:
: $my _static. " ";
$bar = new bar ();
Print $bar->foostatic (). " ";
? >

Example #2 static Method code example

<?php
class Foo {public
  static function Astaticmethod () {
    //...
  }
}}
Foo::astaticmethod ();
$classname = ' Foo ';
$classname:: Astaticmethod (); As of PHP 5.3.0
?>

A summary of static variables and static methods in static

static variables

A static variable is a variable that exists only in the scope of a function, but the value of such a variable is not lost when the function is executed, that is, the variable will still remember the original value the next time the function is called. To define a variable as static, simply precede the variable with the static keyword.

In a class, the static keyword has two main usages, one for defining static members and one for defining static methods. Within a class, you can use the Scope qualifier (::) To access variables of different levels of scope.

static method

There is an important difference between static and Non-static methods: When you call a static method, you no longer need to have an instance of the class.

Static and Non-static methods use the principle: one is if a method does not contain $this variables, should be static method, if you do not need an instance of the class, you might also use a static class, so you can eliminate the work of instantiating the class. Alternatively, you cannot use the $this variable in a static method because the static method does not belong to a particular instance.

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.