• 构造函数2


    <?php

    /**
    * Created by IntelliJ IDEA.
    * User: Administrator
    * Date: 2017/1/21
    * Time: 14:16
    * public,private,protected的区别
    *public:权限是最大的,可以内部调用,实例调用等。
    *protected: 受保护类型,用于本类和继承类调用。
    *private: 私有类型,只有在本类中使用。
    */
    error_reporting(E_ALL);

    class test{
    public $public;
    private $private;
    protected $protected;
    static $instance;
    public function __construct(){
    $this->public = 'public <br>';
    $this->private = 'private <br>';
    $this->protected = 'protected <br>';
    }
    static function tank(){
    if (!isset(self::$instance[get_class()]))
    {
    $c = get_class();
    self::$instance = new $c;
    }
    return self::$instance;
    }
    public function pub_function() {
    echo "you request public function<br>";
    echo $this->public;
    echo $this->private; //private,内部可以调用
    echo $this->protected; //protected,内部可以调用
    $this->pri_function(); //private方法,内部可以调用
    $this->pro_function(); //protected方法,内部可以调用
    }
    protected function pro_function(){
    echo "you request protected function<br>";
    }
    private function pri_function(){
    echo "you request private function<br>";
    }
    }
    $test = test::tank();
    echo $test->public;
    echo $test->private; //Fatal error: Cannot access private property test::$private
    echo $test->protected; //Fatal error: Cannot access protected property test::$protected
    $test->pub_function();
    $test->pro_function(); //Fatal error: Call to protected method test::pro_function() from context
    $test->pri_function(); //Fatal error: Call to private method test::pri_function() from context

  • 相关阅读:
    【初入职场】工作一个月
    sql中更新数据库用到declare @a in
    Mac 下ll命令 command not found
    GLIBC_2.7升级
    PHP 5.5以后加速插件:Zend Opcache
    ssh的public key的使用
    apache Alias使用问题
    linux下telnet mysql的3306断口,提示Can't connect to MySQL server on localhost (110)
    知乎技术方案初探[转]
    JS刷新父窗口的几种方式
  • 原文地址:https://www.cnblogs.com/youxianyen/p/6339437.html
Copyright © 2020-2023  润新知