Analysis of the implementation of properties and class constants in PHP object oriented programming

Time:2021-1-16

In this paper, an example of PHP object-oriented programming such as properties and class constants. The details are as follows:

Properties

Class variable members are called “attributes”, or “fields” and “features”, which are collectively called “attributes” in this document. Attribute declaration starts with the keywords public, protected or private, followed by a common variable declaration. The variables in the attribute can be initialized, but the initialization value must be a constant. The constant here means that the PHP script can get its value at the compilation stage, and it does not depend on the information of the runtime.

For more details on public, protected, and private, see access control (visibility).

Note:

In order to be backward compatible with PHP 4, PHP 5 can still directly use the keyword VaR to replace (or attach to) public, protected or private. But VaR is no longer needed. In PHP 5.0 to 5.1.3, VaR is considered obsolete and E is thrown_ Strict warning, but after 5.1.3, it is no longer considered abandoned and no warning will be thrown.

If you use VaR to declare a property directly instead of using one of public, protected or private, PHP 5 will treat it as public.

In the member method of a class, you can use – > (object operator): $this – > property (where property is the property name) to access non static properties. Static properties are accessed with:: (double colon): Self:: $property. For more information about the differences between static and non static attributes, see static keyword.

When a method is called inside a class definition, there is an available pseudo variable $this. $this is a reference to the calling object (usually the object to which the method belongs, but if it is statically called from a second object, it may also be another object).

Property declaration and call

<?php
class SimpleClass
{
  //Bad property declaration
  public $var1 = 'hello ' . 'world';
  public $var2 = <<<EOD
hello world
EOD;
  public $var3 = 1+2;
  public $var4 = self::myStaticMethod();
  public $var5 = $myVar;

  //Correct property declaration
  public $var6 = myConstant;
  public $var7 = array(true, false);

  //After PHP 5.3.0, the following statement is also true
  public $var8 = <<<'EOD'
hello world
EOD;
  //Property call
  public getvar1() {
    echo $this->var1;
  }
}

$simpleObject = new $SimpleClass();
//Object call properties
Echo $simpleobject - > VAR1; // output "Hello world"

Class constants

You can define a constant as a value that always remains unchanged in a class. You don’t need to use the $sign to define and use constants.

The value of a constant must be a fixed value, not a variable, class attribute, the result of a mathematical operation or a function call.

Constants can also be defined in interfaces. See the interface section of the document for more examples.

Since PHP 5.3.0, you can use a variable to dynamically call classes. However, the value of the variable cannot be a keyword (such as self, parent or static).

Define and use a class constant

<?php
class MyClass
{
  const constant = 'constant value';

  function showConstant() {
    echo self::constant . "\n";
  }
}

echo MyClass::constant . "\n";

$classname = "MyClass";
Echo $classname:: constant. "\ n"; // since 5.3.0

$class = new MyClass();
$class->showConstant();

Echo $class:: constant. "\ n"; // since PHP 5.3.0

More about PHP related content, interested readers can see the special topics of this website: “PHP object-oriented programming introductory tutorial”, “PHP array operation skills encyclopedia”, “PHP basic grammar introductory tutorial”, “PHP operation and operator Usage Summary”, “PHP string Usage Summary”, “PHP + MySQL database operation introductory tutorial” and “PHP common database operation” Summary of writing skills

I hope this article is helpful for PHP programming.