Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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_Apache_Oop_Model View Controller_Symfony1 - Fatal编程技术网

Php 为什么我可以调用非静态函数而不声明类对象?

Php 为什么我可以调用非静态函数而不声明类对象?,php,apache,oop,model-view-controller,symfony1,Php,Apache,Oop,Model View Controller,Symfony1,我使用的是symfony1.0,在我的project/lib文件夹中有这个MyClassInc.class.php class MyClassInc { public function validateFunction ($params) { // my codes } static function testFunction ($params){ // my codes } } 然后,在我的project/apps/myapps/modules/actions

我使用的是symfony1.0,在我的
project/lib
文件夹中有这个
MyClassInc.class.php

class MyClassInc {
  public function validateFunction ($params) {
    // my codes
  }
  static function testFunction ($params){
    // my codes
  }
}
然后,在我的
project/apps/myapps/modules/actions
中执行我的操作
actions.class.php

class inventoryCycleCountActions extends sfActions
{
  public function validateOutstandingTransaction () {
    $res0 = MyClassInc :: validateFunction($param); // It works
    $res1 = MyClassInc :: testFunction($param); // It works
    $myClass = new MyClassInc();
    $res2 = $myClass->validateFunction($param); // still works
    $res3 = $myClass->testFunction($param); // still works, da hell?
  }
}
我试图清除缓存文件夹以进行重新测试,但似乎所有这些都可以正常工作

问题: 所以为什么?我应该用哪一个?它对性能或其他方面有影响吗

更新1:

class MyClassInc {
  public function isProductValidated ($product){
    return true;
  }
  public function validateFunction ($params) {
    // IF, I call by using "$res0".. Throws error
    //
    $this->isProductInLoadPlans($product);
  }
}
如果我通过$res0调用validateFunction,它将抛出以下错误:

sfException:调用未定义的方法 inventoryCycleCountActions::isProductValidated

,如果我通过$res2调用它,它工作正常

因为,我目前使用的是$res0,所以我必须像这样调用该方法

MyClassInc::iProductValidated($product)


->
之间唯一的真正区别是如何处理
$this
。使用
::
函数将具有调用方作用域中定义的
$this

class A {

  public function foo() {
    A::bar();
    A::foobar();
  }

  static private function bar() {
     // $this here is the instance of A
  }

  static public function foobar() {
    // Here you can have anything in $this (including NULL)
  }
}

$a = new A;
$a->foo();
$a->foobar(); // $this == $a but bad style
A::foobar(); // $this == NULL
当您想使用实例方法时,应该使用
->
,因为这样可以正确解析实例方法(包括继承)<代码>:将始终调用指定类的方法


我相信现在已经做出了努力,强制只调用静态方法,而只调用动态方法,以避免混淆。

看一看,基本上,PHP会警告您(如果您启用了错误报告)。但是,如果静态调用非静态函数,则无法访问任何实例变量。因此,如果您尝试使用仅适用于对象的任何变量(即实例变量),您将得到一个致命错误。感谢您的回复dave。。你提到的线程非常有用,正因为如此,我对如何实现我的这个类更加困惑。使用::调用非静态函数有什么缺点吗?据我所知,当静态调用实例方法时,PHP只会生成一个警告。如果客观地调用静态方法,则不会出现错误。所以你总是可以通过
->
逃脱惩罚。那么,我有什么建议吗?如果你的函数是静态的,请使用::。如果函数不是静态的,请使用->。