Using instances of static methods in PHP classes

Source: Internet
Author: User
Tags mkdir php class

PHP is no exception! A static method (property) is a property or method that is annotated with a static keyword (for example, a static property public static username;)
The biggest difference between static and Non-static methods is that their life cycle is different, using an example to illustrate

static method definition

Defining a static method is simple, adding static before declaring a keyword function, for example:

The code is as follows Copy Code

Class A
{
static function Fun ()
{
Do somathing
}
}

Static methods use

When used in the same time as static variables, do not need to instantiate, directly with:: Call, for example:

The code is as follows Copy Code

A::fun ()

Comparison of common methods
Because the call to a static method does not need to be instantiated, it is wrong to refer to a property or method of the class itself in a static method, i.e., the form self and $this.

The code is as follows Copy Code

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 results:

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 isn't in the object context in/users/lch/program/php/class_method.php on line 14

Look at one more example

Use an example to illustrate.

The code is as follows Copy Code

Class user{
public static $username; Declaring a static property
Public $password; Declaring a Non-static property

function __construct ($pwd) {
Echo ' Username: ', Self:: $username; Output static properties

Self:: $username = ' admin '; Assigning values to static properties
$this->password = $pwd; Assigning Values to non-static properties
}

Public function Show () {//Output class Property
Echo '
Username: ', Self:: $username;
Echo '
Password: ', $this->password;
}

public static function Sshow () {
Echo '
Username: ', Self:: $username;
Echo '
Password: ', $this->password;
}
}
User:: $username = ' root '; Assigning values to static properties of the assignment user class
$objUser = new User (' 123456′); Instantiating the user class
$objUser->sshow ();
Unset ($objUser);
Echo '
Username: ', User:: $username;

/*
* Output results are:
* Username:root
* Username:admin
* password:123456
* Usern
Ame:admin
* */

As you can see from the examples here, static properties work before class instantiation, and static properties can still work when an object is destroyed!
Also because of this property of a static method, you cannot call a Non-static property or method in a static method

Then look.

1, in the PHP class, assuming that all properties and method visibility is public, the external access to the class's methods or properties must be invoked through the object "class instantiation process."

eg

The code is as follows Copy Code

Class Log
{
Public $root = Directory_separator;
Public $logPath = '/data/app/www/test-realtime.monitoring.c.kunlun.com/log ';
Public $defaultDir = ' 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);
}
}


The process of instantiating an object for a class: $LOGOBJ = new Log ();

Methods in the Access class: $logObj->writelog ($param 1, $param 2, $param 3, $param 4);

To access a property in a class: Echo $LOGOBJ->root;

2. If a property in a class is decorated with a static keyword, it cannot access a static-decorated property through the object, but it can be accessed through the object or through the class name:: Method Name, if the method in the class is decorated with static.

3, if the method in the class is static modification, the method can not be used $this, $this refers to the class instantiation object, because the static method can not be called through the object, so 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.