mvc-php static variables, members, and methods

Source: Internet
Author: User

In summary, the use of static members is to ensure that the script cycle is not reinitialized, so avoid the instantiation of the class, but instead of using the class name directly call or access (the first call or access to the time of the generation of a singleton object, and then call directly back to the singleton object).

In MVC, it is often used as a factory pattern to generate a table model class singleton object, and a static $list array member is set in the factory class to hold all the generated table model Singleton. The following Factory.class.php (factory class will be loaded automatically):

Class factory{    public static function m ($class _name= ") {         static  $object _list=array ();  //is not emptied because it is a static member. Note that the object data type         if  (Isset ($object _list[$class _name]) is saved here  {            //if it already exists, return the object directly              return  $object _list[$class _name];         }else{            The  //object array does not exist, and the first call instantiates the class and saves the object in the array               $object _list[$class _name]=new  $class _name ();             return  $object _list[$class _name];  //Returns the object that was just generated          }     }} 

This uses $xx=factory::m (' table name ') when assigning values; You can get a singleton object. The so-called Factory mode listen to tall, in fact, is an object warehouse class bar, for the table structure is not complex small web development, feel the Factory mode is not much use, as require_once and then new to direct. And the factory model is said to guarantee a single case, but after all, there is a factory object it.


========

static variables

is a variable that exists only in the scope of a function, however, after the function is executed, the variable , which means that 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  static  keyword.


define static members
, one for Define a static method . Within the class, you can use the Scope qualifier   (::) To access variables of different levels of scope.

Static Members
A static member is a class variable that can be viewed as abelongs to the entire classInstead of belonging to an instance of the class. Unlike a typical instance variable, a static member retains only one variable value, and the value of the variableis valid for all instances, in other words, all instances share this member.
$this represent only the current instance of the class,andSelf:: Represents the class itself, this operator cannot be used in code other than the class, and it does not recognize its position in the inheritance tree hierarchy.
in other words, the extension class uses the Selfscope, SelfA method declared in a base class can be called, but it is always called by methods that have been overridden in the extension class.$thisThe difference is that when you use a static variable, you must add it after the scope qualifier$symbols.
In an extension class, when the method of the base class is overridden, use theParentThe scope calls the method defined in the base class.static members can also belong to the parent class onlyIf you declare a member in both the subclass and the parent class, you can also use theparant::accesses a variable in a parent class in a subclass. In this case, the static members of the parent class and the child class are saved with different values.
You can access a member statically by writing the name of the class on the left side of the: operator, soavoid creating instances of classesnot only the code that instantiates the class is omitted, but alsomore efficient, because each instance of the class consumes a fraction of the system resources.
when accessing member variables using:: operator, you need to pay attention to the$use of symbols. Because PHP does not currently support the use of dynamic static variables, that is, mutable static variables are not supported. When using the $this-> $var, the member being accessed is the value of the variable contained in the $var.instead of accessing a variable without the $ symbol, you are actually looking for a constant of a classand constants are not accessible through $this ..
Static in PHP6: scope makes it unnecessary for us to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static::, This qualifier calculates the members of the last class on the inheritance hierarchy immediately before the code executes. One process is called deferred binding, which allows us to override a static variable in a subclass, and can also ask the final member from a function declared in the parent class.

Static Methods
There is an important difference between static and non-static methods: When a static method is called,you no longer need to have instances of classes.
static and non-static methodsPrinciples of Use:
one is that if a method does not contain a $this variable, it should be a static method, and if an instance of the class is not required, you might also want to use a static class, which avoids the task of instantiating the class. Also, you cannot use the $this variable in a static method, because the static method does not belong to a particular instance.

When using scope-qualified operators in PHP, variables are not allowed when they are used as the name of the class.


mvc-php static variables, members, and methods

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.