Usage of $this, static, final, const, self in PHP

Source: Internet
Author: User
Tags php class

This article is the main part of the PHP class on the $this,static,final,const,self of these key words to use the method.

$this

$this represents the current instance, using the form of $this->value= ' phpernote ' when the internal method of the class accesses an attribute that is not declared const and static. Common uses such as:

$this-> Property

$this-> method

Examples are as follows:

  code is as follows copy code
<?php
Class myclass{
 private $name
 public  function __construct ($name) {
   $this- >name= $name;
 }
 public function GetName () {
  return $this->name;
 }
 public  function Printname () {
  echo $this->getname ();
 }
}
$myclass = new MyClass ("I like Www.111cn.net ");
$myclass->printname ()//output: I like www.111cn.net


There are three ways to call the properties and methods of the current class inside a class. Is self, parent, $this, which is the difference between the three keywords: self is used to point to the current class; parent is used to point to the parents of the current class, and you can use that keyword to invoke the properties and methods of the parent class; $ This is used to call its own properties and methods in the class body.

Static

The keyword can be the class name of the static member that is the self (used when calling static members within the Class) (used when calling static members inside a class outside of the Class)

Declare a static variable as follows:

static $val = ';

Variables that exist only in the scope of the function, the value of the variable will not be lost after the function is executed, only once, initialization static variables cannot use the expression, instead of global variables because global variables are easily maintained by all function accesses.

Using static in a class has two main uses, defining static members, and defining static methods. A static member retains only the value of one variable, which is valid for all instances, as follows:

The code is as follows Copy Code
<?php
Class myobject{
public static $myStaticVar = 0;
function MyMethod () {
Self:: $myStaticVar +=2;
echo self:: $myStaticVar;
}
}
$instance 1=new MyObject ();
$instance 1->mymethod ();
$instance 2=new MyObject ();
$instance 2->mymethod ();

The results will print 2, 4, respectively.

The code is as follows Copy Code

<?php
Class book{
static $num = 0;
Public Function showMe () {
echo "You are dripping." Self:: $num. " Visitors ";
Self:: $num + +;
}
}
$book 1=new Book ();
$book 1->showme ();
echo "<br>";
$book 2=new Book ();
$book 2->showme ();
echo "<br>";
echo "You are dripping". Book:: $num. " Visitors ";
?>


The result will be:

You are 0 guests
You are 1 guests
You are 2 guests

It is also important to note that if the method of the class is static, the properties that he accesses must be static.

Final

The final keyword in PHP can modify the methods in the class as well, but they work the same way, that is, if you use the final keyword to decorate, the modified class or method will not be extended or inherited. You just have to quote it honestly. If you use final in front of the class, this means that the class cannot use inheritance; if you use the PHP final keyword before the method, this means that the method cannot be overwritten. The reason is so simple, let us also look at a simple example.

The final class and method, which cannot be inherited, cannot be overridden by this keyword-decorated method. The general usage is as follows:

<?php
Final class myclass{//This class will not be allowed to be inherited
Final function fun1 () {...} This method will not be allowed to be overridden
}

Cases

The code is as follows Copy Code
<? Php
Final class BaseClass {
Public Function test () {
echo "Baseclass::test () Calledn";
}
Final public Function moretesting () {
echo "baseclass::moretesting () Calledn";
}
}
Class ChildClass extends BaseClass {
Public Function moretesting () {
echo "childclass::moretesting () Calledn";
}
}
Results in Fatal error:cannot override final Method Baseclass::moretesting ()
?>

Const

When the internal method of a class accesses a property that has been declared as const and static, it needs to be called using the self:: $name. Examples are as follows:

The code is as follows Copy Code
<?php
Class clss_a{
private static $name = "Static Class_a";
Const pi=3.14;
Public $value;
public static function GetName () {
Return self:: $name;
}
This is incorrect, and static methods cannot access non-static properties
public static function GetName2 () {
Return self:: $value;
}
Public Function Getpi () {
Return self::P i;
}
}

Note that the Const attribute's declaration format is const pi=3.14, not const $PI = 3.14.

Self

Self represents the class itself, pointing to the current class. Static members, methods, and constants that are commonly used to access classes.

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.