Home  >  Article  >  Backend Development  >  php: access control and Static keyword

php: access control and Static keyword

黄舟
黄舟Original
2017-07-02 10:52:201503browse

1. PhpAccess control

Access control includes: public, private, protected

Access control (visibility)

Yes Access control of properties or methods is achieved by adding the keywords public, protected or private in front of it. Class members defined as public can be accessed from anywhere. A class member defined as protected can be accessed by itself, its subclasses, and its parent class. Class members defined as private can only be accessed by the class in which they are defined.

Access control of attributes

Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.

Example #1 Property declaration

<?php
/**
 * Define MyClass
 */
class MyClass
{
    public $public = &#39;Public&#39;;
    protected $protected = &#39;Protected&#39;;
    private $private = &#39;Private&#39;;
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj = new MyClass();
echo $obj->public; // 这行能被正常执行
echo $obj->protected; // 这行会产生一个致命错误
echo $obj->private; // 这行也会产生一个致命错误
$obj->printHello(); // 输出 Public、Protected 和 Private
/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // 可以对 public 和 protected 进行重定义,但 private 而不能
    protected $protected = &#39;Protected2&#39;;
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj2 = new MyClass2();
echo $obj2->public; // 这行能被正常执行
echo $obj2->private; // 未定义 private
echo $obj2->protected; // 这行会产生一个致命错误
$obj2->printHello(); // 输出 Public、Protected2 和 Undefined
?>

Note: For compatibility reasons, the method of using the var keyword to define variables in PHP 4 is still valid in PHP 5 (just as a public key an alias for a word). In versions prior to PHP 5.1.3, this syntax produced an E_STRICT warning.

Method access control

Methods in a class can be defined as public, private or protected. If these keywords are not set, the method defaults to public.

Example #2 Method declaration

<?php
/**
 * Define MyClass
 */
class MyClass
{
    // 声明一个公有的构造函数
    public function construct() { }
    // 声明一个公有的方法
    public function MyPublic() { }
    // 声明一个受保护的方法
    protected function MyProtected() { }
    // 声明一个私有的方法
    private function MyPrivate() { }
    // 此方法为公有
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // 这行能被正常执行
$myclass->MyProtected(); // 这行会产生一个致命错误
$myclass->MyPrivate(); // 这行会产生一个致命错误
$myclass->Foo(); // 公有,受保护,私有都可以执行
/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // 此方法为公有
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); // 这行会产生一个致命错误
    }
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // 这行能被正常执行
$myclass2->Foo2(); // 公有的和受保护的都可执行,但私有的不行
class Bar 
{
    public function test() {
        $this->testPrivate();
        $this->testPublic();
    }
    public function testPublic() {
        echo "Bar::testPublic\n";
    }
    
    private function testPrivate() {
        echo "Bar::testPrivate\n";
    }
}
class Foo extends Bar 
{
    public function testPublic() {
        echo "Foo::testPublic\n";
    }
    
    private function testPrivate() {
        echo "Foo::testPrivate\n";
    }
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>

Access control of other objects

Objects of the same class can access each other's private and protected members even if they are not the same instance . This is because the internal implementation details of these objects are known.

Example #3 Access private members of the same object type

<?php
class Test
{
    private $foo;
    public function construct($foo)
    {
        $this->foo = $foo;
    }
    private function bar()
    {
        echo &#39;Accessed the private method.&#39;;
    }
    public function baz(Test $other)
    {
        // We can change the private property:
        $other->foo = &#39;hello&#39;;
        var_dump($other->foo);
        // We can also call the private method:
        $other->bar();
    }
}
$test = new Test(&#39;test&#39;);
$test->baz(new Test(&#39;other&#39;));
?>

The above routine will output:

string(5) "hello"
Accessed the private method.

3 Static keyword

Static (static ) Keyword

Tip

This page explains the use of the static keyword to define static methods and properties. Static can also be used to define static variables and late static binding. See the above page to see how static is used there.

Declaring a class attribute or method as static allows you to access it directly without instantiating the class. Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can).

In order to be compatible with PHP 4, if no access control is specified, properties and methodsdefault to public.

Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.

Static properties cannot be accessed by objects through the -> operator.

Calling a non-static method statically will result in an E_STRICT level error.

Like all other PHP static variables, static properties can only be initialized to literals or constants, and cannot use expressions. Therefore, a static property can be initialized to an integer or an array, but it cannot be initialized to another variable or function return value, nor can it point to an object.

Since PHP 5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.

Example #1 Static attribute example

<?php
class Foo
{
    public static $my_static = &#39;foo&#39;;
    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 
print $foo::$my_static . "\n";
$classname = &#39;Foo&#39;;
print $classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
   </programlisting>
  </example>
  <example>
   <title>静态方法示例</title>
    <programlisting role="php">
<![CDATA[
<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}
Foo::aStaticMethod();
$classname = &#39;Foo&#39;;
$classname::aStaticMethod(); // 自 PHP 5.3.0 起
?>

The above is the detailed content of php: access control and Static keyword. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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