php, and = and: usage and use of self and $this

Source: Internet
Author: User

=
The relationship between the key and value used for an array in an array
For example:
$a = Array (
' 0 ' = ' 1 ',
' 2 ' = ' 4 ',
);

echo $a [' 0 '];
echo $a [' 2 '];


-
Methods and properties in a class to reference class instances
For example:
Class test{
function Add () {return $this->var++;}
var $var = 0;
}

$a = new Test; Instantiating object names
echo $a->add ();
Echo $a->var;


::
Methods for referencing static and static properties in a class
For example
Class test{
public static function test () {
public static $test = 1;
}
}

The static and static properties of a class can be used directly without instantiating the object (using the class name:: Static method Name)

Test::test (); Call the static method test
Test:: $test; To get the value of the $test static property

Note:
Static methods are instantiated and put into memory when they are read to this class or introduced into the class, and non-static classes need new.
Static classes have only one copy of static properties in memory, even if there are multiple instances.


= = selef=== $this ======

Self is the class name that references the static class, and $this is the instance name that references the non-static class

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

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

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

All instances of a class, a static property in a common class.

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

The following example sets a counter $count property, setting the private and static adornments.
This way, the $count property is not directly accessible to the outside world. And the results of the program run we also see multiple instances using the same static $count property.
<?php
Class User
{
private static $count = 0; Logs the logon status of 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 has". $user 1->getcount (). "User";
echo "<br/>";
Unset ($user 3);
echo "Now here has". $user 1->getcount (). "User";
?>

Static properties are called directly
Static properties can be used directly without instantiation and can be used directly when the class is not created.

Used in the following way: 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 precisely.
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 was the static property created in memory? No data was found in PHP.
Referring to the concepts in Java, the explanation should also be universal. Static properties and methods that are created when the class is invoked.

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

The way to use the class name:: Static method Name

Let's continue writing this math class for mathematical calculations. We design a method to figure out the maximum value.
Since it is a mathematical operation, it is much more convenient if the method can be taken over.
We're just doing this class to demonstrate the static method design. 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;
}
}
$a = 99;
$b = 88;
Echo Displays the maximum value in $a and $b is ";
echo "<br/>";
Echo Math::max ($a, $b);
echo "<br/>";
echo "<br/>";
echo "<br/>";
$a = 99;
$b = 100;
Echo Displays the maximum value in $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 value 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 = 99;
$b = 77;
$c = 88;
echo "Displays the maximum value in the $a $b $c is";
echo "<br/>";
Echo math::max3 ($a, $b, $c);
?>

static method call static property
Use self:: Call the static property of this class.

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

Self:: And ¥this cannot invoke a non-static property in a static method
A non-static method in the class is self:: When called, the system automatically converts this method to a static method


1. Static methods cannot invoke non-static properties. You cannot use Self:: Call a non-static property.

<?php
This is the wrong way.
Class Circle {
Public $pi = 3.14;
public static function Circleacreage ($r) {
return $r * $r * self::p i; Error, non-static property cannot be called.
}
}
$r = 3;
echo "Radius $r the area of the circle is". Circle::circleacreage ($R);
?>


2. You cannot also use $this to get the value of a non-static property.
In PHP5, a non-static method cannot be called using $this in a static method.
<?php
The math class that implements the maximum value 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); Non-static methods cannot be called with $this.
$num 2 = $this->max ($num 2, $num 3); //
$num 1 = $this->max ($num 1, $num 2); //
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 188;
echo "Displays the maximum value in the $a $b $c is";
echo "<br/>";
Echo math::max3 ($a, $b, $c); The same one will make an error.
?>

3. When a non-static method in a class is self: called, the system automatically converts the method to a static method.

<?php
The math class that implements the maximum value 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 = 99;
$b = 77;
$c = 188;
echo "Displays the maximum value in the $a $b $c is";
echo "<br/>";
Echo math::max3 ($a, $b, $c);
?>

---------------------------------------------
The meaning of this in PHP

Class User {
Public $name; function GetName () {
Echo $this->name;
}
}

php, and = and: usage and use of self and $this

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.