duanjia3187
2016-01-21 16:34
浏览 40
已采纳

PHP静态与对象调用

So I have this problem where I can call an object method statically and vice versa. Is this supposed to happen or what am I doing wrong in case?

PHP Version: 5.6.12 XAMPP Version: 3.2.1

function endl()
{
    echo "<br>";
}

class Base
{
    public function objectFunc($msg)
    {
        echo "You called a non-static function from " . $msg;
        endl();
    }

    public static function staticFunc($msg)
    {
        echo "You called a static function from " . $msg;
        endl();
        }
    }

Base::objectFunc("a static call");
Base::staticFunc("a static call");
$base = new Base;
$base->objectFunc("a non-static call");
$base->staticFunc("a non-static call");


Here are the results from running this:

You called a non-static function from a static call
You called a static function from a static call
You called a non-static function from a non-static call
You called a static function from a non-static call

图片转代码服务由CSDN问答提供 功能建议

所以我有这个问题,我可以静态调用对象方法,反之亦然。 这是应该发生的还是我做错了以防万一?</ p>

PHP版本:5.6.12 XAMPP版本:3.2.1 </ p>

 <  code> function endl()
 {
 echo“&lt; br&gt;”; 
} 
 
class Base 
 {
 public function objectFunc($ msg)
 {
 echo“你叫非 来自“的静态功能。  $ msg; 
 endl(); 
} 
 
公共静态函数staticFunc($ msg)
 {
 echo“您从中调用了静态函数”。  $ msg; 
 endl(); 
} 
} 
 
Base :: objectFunc(“静态调用”); 
Base :: staticFunc(“静态调用”); 
 $ base = new Base  ; 
 $ base-&gt; objectFunc(“非静态调用”); 
 $ base-&gt; staticFunc(“非静态调用”); 
 
 
以下是运行此操作的结果:  
 
您从静态调用调用非静态函数
您从静态调用调用静态函数
您从非静态调用调用非静态函数
您从非静态调用调用静态函数 
 </ code> </ pre> 
 </ div>

2条回答 默认 最新

相关推荐 更多相似问题