Home > Backend Development > PHP Tutorial > Discuss the detailed explanation of static keywords and class constants in OO in PHP_PHP Tutorial

Discuss the detailed explanation of static keywords and class constants in OO in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:07:57
Original
812 people have browsed it

When I had nothing to do, I just looked for some information about OO in PHP, access control modifiers, several keywords such as self, parent, const, static, and arrow operators (also called so in the book...that is, " ->"), range parsing operator (i.e. double colon "::"), but I think this is much the same as OO in C# language, and it is easy to understand. However, we should still take a look to clarify the OO ideas in PHP. Yes.
-------------------------------------------------- ----------------------------------
Declare static class members and methods so that it does not require a Instances of classes. A declaration of a static member cannot be accessed through an instance of a class object (although a static method can).
Static declarations must come after visibility declarations. For compatibility with PHP 4, if no visibility is declared, then members and methods will be treated as if they had been declared public.
Since static methods can call non-object instances, the pseudo variable $this cannot be used in methods declared as static.
In fact, the static method calling form is determined at compile time. When using a class name that must be declared, the approach is full identification and no inheritance rules apply. This approach is fully valid when using class names that must be declared, and there are no rules for using inheritance.
If self has been declared, then self is interpreted by the class it currently belongs to. Nor does inheritance rules apply. Static properties cannot be accessed from non-static methods through the arrow operator ->., which will generate an E_STRICT level warning.

Copy code The code is as follows:

class Foo
{ public static $my_static= 'foo';
public function staticValue(){ return self::$my_static; }
}
class Bar extends Foo
{ public function fooStatic(){ return parent::$my_static; }
}
print Foo::$my_static."/n";
$foo = new Foo();
print $foo->staticValue()."/n";
print $foo->my_static."/n";// Undefined "Property" my_static
// $foo::my_static is not possible
print Bar::$my_static."/n";
$bar = new Bar();
print $bar->fooStatic()."/n";
?>

Copy code The code is as follows:

//Static method example (Static method example)
class Foo
{ public static function aStaticMethod() { }
}
Foo::aStaticMethod();
?>

You can define constants in each base class to make it unchanged. A constant is different from an ordinary variable in that you don't use the $ symbol to declare or use it. Like static members, a constant value cannot be accessed through an instance of an object (use $object::constant instead). The constant value must be a constant expression, not a variable, a member of a class, a mathematical expression, or a function The result of the call.
Copy code The code is as follows:

class MyClass
{ const constant = 'constant value';
function showConstant() { echo self::constant."/n"; }
}
echo MyClass::constant."/n";
$class = new MyClass( );
$class->showConstant();// echo $class::constant; is not allowed
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327503.htmlTechArticleWhen I had nothing to do, I just looked for some information about OO in PHP. Access control modifier, self, parent, const, static keywords, arrow operator (also called this in the book...that is, "-"),...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template