PHP class static method use instance _ PHP Tutorial

Source: Internet
Author: User
Use an instance for static methods in the PHP class. In php, we can directly add a static method before a function or variable. it is similar to a static variable in use and does not need to be instantiated. we can directly use: called, in php, we can add a static method directly before a function or variable. it is similar to a static variable and does not need to be instantiated. we can directly use the static method :: called. I will give you a few static method instances.

PHP is no exception! The so-called static method (attribute) is the attribute or method marked with the static keyword (for example, the static property public static username ;)
The biggest difference between static and non-static methods is that they have different lifecycles.

Static method definition

It is easy to define the static method. before declaring the keyword function, add static, for example:

The code is as follows:

Class
{
Static function fun ()
{
// Do somathing
}
}

Static method usage

It is similar to a static variable and does not need to be instantiated. it can be called directly using:. for example:

The code is as follows:

A: fun ()

Comparison of common methods
Because the call to a static method does not need to be instantiated, an error occurs when you reference the attributes or methods of the class in the static method, that is, self and $ this are incorrect.

The code is as follows:

Class MyClass
{
Public $ num = 5;

Function _ construct ()
{
$ This-> num = 10;
}

Function fun_1 ()
{
Echo "I am a public method named fun_1.n ";
Echo "The num of object is {$ this-> num}. n ";
}

Static function fun_2 ()
{
Echo "I am a static method named fun_2.n ";
}

Function fun_3 ($ n)
{
Echo "The arg is {$ n} n ";
}
}

$ M = new MyClass;
$ M-> fun_1 ();
$ M-> fun_2 ();
$ M-> fun_3 ('test ');

MyClass: fun_1 ();
MyClass: fun_2 ();
MyClass: fun_3 ('test ');

Output result:

Lch @ localhost: php $ php class_method.php
I am a public method named fun_1.
The num of object is 10.
I am a static method named fun_2.
The arg is test
I am a public method named fun_1.
PHP Fatal error: Using $ this when not in object context in/Users/lch/program/php/class_method.php on line 14

Let's look at another instance.

Use an instance.

The code is as follows:

Class user {
Public static $ username; // declare a static attribute
Public $ password; // declare a non-static attribute

Function _ construct ($ pwd ){
Echo 'username: ', self: $ Username; // output static attributes

Self: $ username = 'admin'; // assign a value to the static attribute
$ This-> password = $ pwd; // value for non-static attributes
}

Public function show () {// output class attributes
Echo'
Username: ', self: $ username;
Echo'
Password: ', $ this-> password;
}

Public static function sshow (){
Echo'
Username: ', self: $ username;
Echo'
Password: ', $ this-> password;
}
}
User: $ username = 'root'; // assign a value to the static attribute of the user class.
$ ObjUser = new user ('000000'); // instantiate the user class
$ ObjUser-> sshow ();
Unset ($ objUser );
Echo'
Username: ', user: $ username;

/*
* The output result is:
* Username: root
* Username: admin
* Password: 123456
* Usern
Ame: admin
**/

From this example, we can see that static attributes work before class instantiation, and can still play a role when the object is destroyed!
Because of this attribute of the static method, you cannot call non-static attributes or methods in the static method.

See

1. in the php class, if the visibility of all attributes and methods is public, the method or attribute of the external class must be called through the object [class instantiation process.

Eg:

The code is as follows:

Class Log
{
Public $ root = DIRECTORY_SEPARATOR;
Public $ logPath = '/data/app/www/test-realtime.monitoring.c.kunlun.com/log ';
Public $ defadir dir = 'default ';

Public function writeLog ($ logName, $ logType, $ data, $ newDir = FALSE)
{
$ FileName = '';

If (! File_exists ($ this-> logPath ))
{
Mkdir ($ this-> logPath, 0777 );
}

If ($ newDir! = FALSE)
{
@ Mkdir ($ this-> logPath. $ this-> root. $ newDir, 0777 );
$ FileName = $ this-> logPath. $ this-> root. $ newDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
}
Else
{
@ Mkdir ($ this-> logPath. $ this-> root. $ this-> defaultDir, 0777 );
$ FileName = $ this-> logPath. $ this-> root. $ this-> defaultDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
}

File_put_contents ($ fileName, date ('Y-m-d H: I: s'). ''. $ data." n ", FILE_APPEND );
}
}


Class instantiation object process: $ logObj = new Log ();

Methods in the callback class: $ logObj-> writeLog ($ param1, $ param2, $ param3, $ param4 );

Attributes in the response class: echo $ logObj-> root;

2. if the attribute in the class is modified by the static keyword, the object cannot be used to access the property modified by the static keyword, however, if the method in the class is modified statically, the object can be accessed through the class name: method name.

3. if the method in the class is modified statically, $ this cannot be used in the method. $ this refers to the instantiated object of the class. because the static method can be called without passing through the object, therefore, the pseudo variable $ this is not available.

...

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.