Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 静态方法可以访问调用方对象、错误或功能?_Php_Oop_Object_Methods_Static - Fatal编程技术网

Php 静态方法可以访问调用方对象、错误或功能?

Php 静态方法可以访问调用方对象、错误或功能?,php,oop,object,methods,static,Php,Oop,Object,Methods,Static,我正在处理我的应用程序,发现了一些方法的奇怪行为,这些方法以静态方式调用,但并没有定义为扩展同一类的静态方法。最终,此方法可以访问和更改受调用方保护的变量和方法。 下面是我的代码示例: <?php class object { private $version; protected $alteredBy = 'nobody'; public function __construct() { $this->version =

我正在处理我的应用程序,发现了一些方法的奇怪行为,这些方法以静态方式调用,但并没有定义为扩展同一类的静态方法。最终,此方法可以访问和更改受调用方保护的变量和方法。 下面是我的代码示例:

<?php

class object
{
    private $version;

    protected $alteredBy = 'nobody';

    public function __construct()
    {
        $this->version    = PHP_VERSION;

        $this->objectName = get_class($this);

        echo sprintf("<pre><strong>New %s Created</strong>", $this->objectName);

    }

    public function __destruct()
    {
        echo sprintf("</pre><strong>Source Code</strong><div>%s</div>", highlight_file(__FILE__, true));
    }
}

class superApplication extends object
{
    public function __toString()
    {
        echo "\nCalling third party object statically like thirdParty::method()\n";

        echo thirdParty::method();

        echo "\nCalling third party object statically via call_user_func()\n";

        echo call_user_func(array('thirdParty','method'));

        echo sprintf("New Object params\n%s", print_r($this, true));

        return sprintf("%s: done\n", $this->objectName);
    }
}

class thirdParty extends object
{    
    public function method()
    {
        if(is_object($this))
        {
            $this->alteredBy = __CLASS__;

            return sprintf(
                "<span style=\"color:red\">Object '%s' was altered successfully by %s class</span>\n", 
                get_class($this),
                __CLASS__
            );
        }
        else return "Cannot access caller object\n\n";        
    }
}

print new superApplication;
?>

考虑PHP 5.3中的这个例子:

PHP Notice:  Undefined property: B::$a in ... on line 15
NULL
string(1) "B"
string(1) "C"
PHP Strict Standards:  Non-static method C::test() should not be called statically, assuming $this from incompatible context in ... on line 16
NULL
NULL
NULL
发生的情况是,假定
C::test()
$this
指针是
新B()
实例中的
$this
。它的作用就像B的一个成员函数,但是C可以访问

它只能从
A
访问受保护和公共变量,从
B
访问公共变量

注意,在调用
C::test()
之前,
$this->a
触发了一个通知。在调用之后,它不再这样做,因为变量是在调用中创建的。但是在任何时候,
A
的私有变量都是不可访问的


是的,严格来说,这在PHP5.3中被认为是无效的。即使早期版本允许您在没有警告的情况下执行此操作(我没有对此进行检查或研究),您也不应该依赖此行为,因为这显然是对OOP的滥用。

请考虑PHP 5.3中的示例:

PHP Notice:  Undefined property: B::$a in ... on line 15
NULL
string(1) "B"
string(1) "C"
PHP Strict Standards:  Non-static method C::test() should not be called statically, assuming $this from incompatible context in ... on line 16
NULL
NULL
NULL
发生的情况是,假定
C::test()
$this
指针是
新B()
实例中的
$this
。它的作用就像B的一个成员函数,但是C可以访问

它只能从
A
访问受保护和公共变量,从
B
访问公共变量

注意,在调用
C::test()
之前,
$this->a
触发了一个通知。在调用之后,它不再这样做,因为变量是在调用中创建的。但是在任何时候,
A
的私有变量都是不可访问的

是的,严格来说,这在PHP5.3中被认为是无效的。即使早期版本允许您在没有警告的情况下执行此操作(我没有对此进行检查或研究),您也不应该依赖于此行为,因为这显然是对OOP的滥用