Simple analysis of the usage difference between PHP static method and Non-static method _php skill

Source: Internet
Author: User
Tags php programming

In PHP programming, the Static keyword declares that a property or method is related to a class, not to a particular instance of a class, and therefore such a property or method is also called a class property or class method

If access control permission allows, you do not have to create the class object and use the class name plus two colons "::" to invoke it directly.

The static keyword can be used to modify variables, methods.

static properties and static methods in a class can be accessed directly without instantiating it.

Static properties and methods that can access only static properties and methods, and cannot have classes that access non-static properties and methods. Because static properties and methods are created, there may not be any instances of this class that can be invoked.

Static properties, in memory only one copy, shared for all instances.

Use self:: keyword to access static members of the current class.

All instances of a class that share static properties in a class.

That is, even if there are multiple instances in memory, there is only one copy of the static property.

example, set up a counter $count property, set private and static adornments.

In this way, the $count property is not directly accessible to the outside world. The results of a program run also see multiple instances using the same static $count property.

<?php  
class User 
{  
 private static $count = 0;//log login for all users.  
 Public Function __construct () {  
  self:: $count = self:: $count + 1;  
 }  
 Public Function GetCount () {return  
  self:: $count;  
 }  
 Public Function __destruct () {  
  self:: $count = self:: $count-1;  
 }  
}  
$user 1 = new user ();  
$user 2 = new user ();  
$user 3 = new user ();  
echo "Now here have". $user 1->getcount (). "User";  
echo "<br/>";  
Unset ($user 3);  
echo "Now here have". $user 1->getcount (). "User";  

Second, static properties are called directly

Static properties can be used directly without instantiating them and can be used directly when the class is not yet created.

How to use:

Class Name:: Static property name

<?php  
class Math 
{public  
 static $pi = 3.14;  
}  
Find the area of a garden with a radius of 3.  
$r = 3;  
echo "radius is the area of the $r is <br/>";  
echo Math:: $pi * $r * $R;  
echo "<br/><br/>";  
Here I think 3.14 is not accurate enough, I set it more accurate.  
math:: $pi = 3.141592653589793;  
echo "radius is the area of the $r is <br/>";  
echo Math:: $pi * $r * $R;  

Class is not created, static properties can be used directly. When is the static property created in memory?

There is no relevant information in PHP.

Refer to the concepts in Java to explain that they should also be generic. Static properties and methods that are created when a class is invoked.

Third, static method

Static methods do not require that the class being instantiated be used directly.

The method used is class name:: Static method Name

Continue to write this math class for mathematical calculations.

Design a method to calculate the maximum value. Since it is a mathematical operation, there is no need to instantiate this class, if this method can be used for more convenient.

This is just the class designed to demonstrate the static method. The max () function comparison value is provided in PHP.

<?php  
class Math 
{public  
 static function Max ($num 1, $num 2) {return  
  $num 1 > $num 2? $num 1: $num 2;< c5/>}   
}  
$a =;  
$b =;  
echo "shows the maximum value $a and $b is";  
echo "<br/>";  
Echo Math::max ($a, $b);  
echo "<br/>"; 
echo "<br/>"; 
echo "<br/>";  
$a =;  
$b = m;  
echo "shows the maximum value $a and $b is";  
echo "<br/>";  
Echo Math::max ($a, $b);  

How static methods Call static methods

The first example, when a static method calls another static method, uses self::

<?php  
//The math class that implements the maximum comparison.  
class Math 
{public  
 static function Max ($num 1, $num 2) {return  
  $num 1 > $num 2? $num 1: $num 2;  
 }  
 public static function Max3 ($num 1, $num 2, $num 3) {  
  $num 1 = Self::max ($num 1, $num 2);  
  $num 2 = Self::max ($num 2, $num 3);  
  $num 1 = Self::max ($num 1, $num 2);    
  return $num 1;  
 }  
}  
$a =;  
$b =;  
$c =;  
echo "Displays the maximum value of $a $b $c is";  
echo "<br/>";  
Echo math::max3 ($a, $b, $c);  
? >

Static methods Call Static properties

Use self:: Invoke the static properties of this class.

<?php  
//  
class Circle 
{public  
 static $pi = 3.14;  
 public static function Circleacreage ($r) {return  
  $r * $r * self:: $pi;  
 }  
}  
$r = 3;  
The area of the echo "Radius $r circle is". Circle::circleacreage ($R);  

A static method cannot invoke a Non-static property. Cannot use Self:: Invoke Non-static property.

<?php  
//This way is the wrong  
class circle 
{public  
 $pi = 3.14;  
 public static function Circleacreage ($r) {return  
  $r * $r * self::p i;  
 }  
}  
$r = 3;  
The area of the echo "Radius $r circle is". Circle::circleacreage ($R);  

You cannot use $this to get the value of a non-static property.

Static method calls Non-static methods

In PhP5, non-static methods cannot be invoked in static methods using the $this identity.

<?php 
//The math class that implements the maximum comparison.  
class Math 
{Public   
 function max ($num 1, $num 2) {  
  echo "bad<br/>";    
  Return $num 1 > $num 2? $num 1: $num 2;  
 }  
 public static function Max3 ($num 1, $num 2, $num 3) {  
  $num 1 = $this->max ($num 1, $num 2);  
  $num 2 = $this->max ($num 2, $num 3);  
  $num 1 = $this->max ($num 1, $num 2);    
  return $num 1;  
 }  
}  
$a =;  
$b =;  
$c = 188;  
echo "Displays the maximum value of $a $b $c is";  
echo "<br/>";  
Echo math::max3 ($a, $b, $c); The same one will complain. 

When there is a Non-static method in a class that is self:: called, the system automatically converts this method to a static method.

<?php  
//The math class that implements the maximum comparison.  
class Math 
{Public   
 function max ($num 1, $num 2) {return   
  $num 1 > $num 2? $num 1: $num 2;  
 }  
 public static function Max3 ($num 1, $num 2, $num 3) {  
  $num 1 = Self::max ($num 1, $num 2);  
  $num 2 = Self::max ($num 2, $num 3);  
  $num 1 = Self::max ($num 1, $num 2);    
  return $num 1;  
 }  
}  
$a =;  
$b =;  
$c = 188;  
echo "Displays the maximum value of $a $b $c is";  
echo "<br/>";  
Echo math::max3 ($a, $b, $c);  
? >

The above analysis of the static method of PHP and the use of non-static methods is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.