PHP static and const keyword usage Analysis _php tips

Source: Internet
Author: User
Tags mysql database

This example describes the static and const keyword usages in PHP. Share to everyone for your reference, specific as follows:

The member properties and member functions that the static keyword describes in the class are static.

A static member can restrict external access because the static member belongs to the class, not to any object instance.

From the memory point of view, where the object is placed in "heap memory", the object's reference is placed in "stack memory", while the static member is placed in the initialization static segment, when the class is first loaded. Lets you share all the objects in memory. As shown in the following illustration:

<?php
class person{public
 static $myCountry = "China";
 public static function say () {
  echo "My Motherland is:". Self:: $myCountry. <br> ";
 }
}
Output static properties
echo Person:: $myCountry. " <br> ";
Call static method
Person::say ();
Modify static Properties Person
:: $myCountry = "China-Jiangsu";
echo Person:: $myCountry. " <br> ";
? >

The output results are:


My Motherland in China is: China
-Jiangsu

Static methods in a class can only access static properties of a class. A static method in a class cannot access a non-static member of a class. We use self to access static properties in a class. Self is similar to this, except that self represents the class where the static method is located, this is similar, except that self represents the class in which the static method is located, this reference pointer, which represents the object that called the method. The static method is not called by the object, and there is no this reference. There is no this reference to this. Without this, there is no way to invoke other member properties in the class.

Const is a keyword that defines a constant. Use const in a class to define constants. Member properties that are decorated with "const" are accessed in much the same way as members accessed by the "static" modifier, using "class name" and the "self" keyword in the method. However, you do not use the "$" symbol, and you cannot use objects to access it.

<?php
class myclass{
 Const constant = ' constant value ';
 function Showconstant () {
  //method calls constants, no $
  echo self::constant. " <br> ";
 }
}
Class is called directly, without the $
echo myclass::constant. " <br> ";
$class = new MyClass ();
$class->showconstant ();
? >

More interested in PHP related content readers can view the site topics: "Introduction to PHP Basic Grammar", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.