如何在php中将非静态方法调用为静态方法


How can I call a non static method into a static method in php?

我对PHP完全陌生。我做了一件事:

 class Foo  
 {         
    @BeforeSuite
    public static function prePare()
   {           
       $obj = new Foo;
       $obj->iVisit("https://google.com");
     }
  }
 public function iVisit($url)
   {
      if ($url != null) {
            $this -> getSession() -> visit($url);
         }
      else {
           throw new PendingException();
         }
   }
}

但它抛出了这个错误:

PHP致命错误:在非对象上调用成员函数getSession()

编辑:

我的场景是在其他测试用例开始运行之前加载一个URL。所以我选择在@beforesuite函数中加载url,但在behat中,@beforesuite是一个静态函数。我不得不从静态函数中调用iVisit方法。

这个问题还有其他解决办法吗?

它不是php概念,它只是一个编程概念。

您不能只在任何地方调用非静态方法,因为您需要使实例(或引用已存在的instance)来调用该函数。