静态方法与非静态方法 - php

以下是静态方法和非静态方法的php类代码示例。

范例1:

class A{
    //None Static method
    function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>";
        } else {
            echo "\$this is not defined.<br>";
        }
    }
 }

 $a = new A();
 $a->foo();
 A::foo();

 //result
 $this is defined (A)
 $this is not defined.

范例2:

class A{
    //Static Method
    static function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>\n";
        } else {
            echo "\$this is not defined.<br>\n";
        }
    }
 }

 $a = new A();
 $a->foo();
 A::foo();

 //result
 $this is not defined.
 $this is not defined.

我试图找出这两个类之间的区别。

正如我们在非静态方法的结果中看到的那样,定义了“ $ this”。

但是另一方面,即使静态方法都被实例化,也没有定义静态方法的结果。

我想知道为什么自从将它们都实例化后它们会有不同的结果?

您能给我启发一下这些代码上发生了什么。

参考方案

在进行下一步之前:请先习惯于指定对象的属性和方法的可见性/可访问性。而不是写作

function foo()
{//php 4 style method
}

写:

public function foo()
{
    //this'll be public
}
protected function bar()
{
    //protected, if this class is extended, I'm free to use this method
}
private function foobar()
{
    //only for inner workings of this object
}

首先,您的第一个示例A::foo将触发通知(始终静态调用非静态方法会这样做)。
其次,在第二个示例中,调用A::foo()时,PHP不会创建即时实例,也不会在调用$a->foo()时在实例的上下文中调用该方法(这也会发出一个请注意BTW)。从本质上讲,静态函数是全局函数,因为在内部,PHP对象不过是C struct,而方法只是具有指向该结构的指针的函数。至少这就是要旨,more details here

静态属性或方法的主要区别(如果使用得当,则有好处)是它们在所有实例之间共享并且可以全局访问:

class Foo
{
    private static $bar = null;
    public function __construct($val = 1)
    {
        self::$bar = $val;
    }
    public function getBar()
    {
        return self::$bar;
    }
}
$foo = new Foo(123);
$foo->getBar();//returns 123
$bar = new Foo('new value for static');
$foo->getBar();//returns 'new value for static'

如您所见,不能在每个实例上都设置静态属性$bar,如果更改其值,则该更改将应用​​于所有实例。
如果$bar是公开的,则您甚至都不需要实例来更改各处的属性:

class Bar
{
    public $nonStatic = null;
    public static $bar = null;
    public function __construct($val = 1)
    {
        $this->nonStatic = $val;
    }
}
$foo = new Bar(123);
$bar = new Bar('foo');
echo $foo->nonStatic, ' != ', $bar->nonStatic;//echoes "123 != foo"
Bar::$bar = 'And the static?';
echo $foo::$bar,' === ', $bar::$bar;// echoes 'And the static? === And the static?'

查看工厂模式,并(纯粹提供信息)也查看Singleton模式。就Singleton模式而言:此外,google为什么不使用它。 IoC,DI,SOLID是您很快会遇到的缩写。了解它们的含义,并弄清楚为什么他们(以自己的方式)不选择Singletons的主要原因

在php中扩展静态方法 - php

class Foo { public static function foobar() { self::whereami(); } protected static function whereami() { echo 'foo'; } } class Bar extends Foo { protected static function…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP-从最后一个循环中删除逗号 - php

我在循环时有一个PHP,如果是最后一个循环,我想从,中删除​​最后一个逗号echo '],'; while($ltr = mysql_fetch_array($lt)){ echo '['; echo $ltr['days']. ' ,'. $ltr['name…