static keyword-static in PHP

Source: Internet
Author: User

At the time of the grand written examination, there are two problems, a topic is about static, together with the Clone () method;

Static keywords, which can be used to modify variables and methods ;

static variables (attributes):

A static variable can be understood as "only one", no matter how many instances of the class it is created in, it always has only one, only one copy in memory and is shared for all instances.

Here is an example:

<?php
Class dome{
public static $var = 1;
Public Function Myfun () {
Echo ' var is '. Self:: $var. "!";
}
}
echo "Var is". Dome:: $var. "!";
Dome:: $var = 2;
$obj = new Dome ();
$obj->myfun ();
?>

The results of the operation are as follows:

As you can see, when an object of the Dome class is not created, we change the value of static familiarity with Var, and after creating the Dome class object, Var remains the last value to change it.

If you do not use static variables:

<?php
Class dome{
public $var = 1;
Public function read () {
echo $this->var. " \ n ";
}
Public function Change () {
$this->var = 9;
}
}
$obj = new Dome ();
$obj->read ();
$obj->change ();
$obj->read ();
$obj 2 = new Dome ();
$obj 2->read ();
?>

We can see:

Each time a new object is created, the memory is reassigned to the new address and the initialized properties.

Static familiarity with non-static familiarity also has a different:

<?php
Class dome{
public static $var = 1;
}
Echo Dome:: $var. " \ n ";
?>

The results of the operation are as follows:

Outside the class can be directly used:: Call, non-static no (nonsense), and static familiarity can not use this to invoke, because static familiarity does not belong to any object. A static property cannot be accessed by an object through the operator.

static method:

I searched for half a day, most of the online tutorials have said:

The important difference between static and non-static methods is that static methods do not need to create objects to access them.

Well, I tested it myself and found that non-static can also be called:

Both non-static and static methods can invoke static resources, but one thing is for sure, static methods cannot invoke non-static resources because there is no object available for this to invoke.

However, if you use:: Call a non-static method directly, you must ensure that the method does not invoke non-static familiarity (nonsense).

Static keywords:

The static keyword, which is referenced here as a scope. Similar to keywords like parent, self, and so on. Unlike parent and self, the parent refers to the scope of the current class, and self refers to the scope of the currently classes, and static refers to all static scopes, and the subclass overrides the parent class, considering the following example:

Finally, my blog moved to http://www.ebwill.com

static keyword-static in PHP

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.