2011-03-01 86 views
0

总之..问题是......“说什么?”扩展名为“我没有收到错误”帮助理解PHP5错误

严格标准:非静态方法Pyro \ Template :: preLoad()不应该静态调用,假设$ this来自/ opt中的不兼容上下文在线/lampp/htdocs/dc/pyro/app/controllers/admin/courses.php 14

public function actionIndex() { 
    $this->data->users = $this->DB->query("SELECT id, name, description FROM :@courses")->getAll(); 
    $this->data->title = 'Courses'; 
    $this->data->content_area = \Pyro\Template::preLoad('admin/courses/index', $this->data); // Line 14 
} 

模板...它不完全...

<?php 
namespace Pyro; 

class Template { 

    // Stores default master template 
    public static $defaultTemplate = 'template.php'; 

    public function preLoad($template, $page) { 
     ob_start(); 

     include(VIEWS . "{$template}.php"); 

     $buffer = ob_get_contents(); 
     @ob_end_clean(); 
     return $buffer; 
    } 

    public function load($page) { 
     include(VIEWS . self::$defaultTemplate); 
    } 
} 

为什么这个错误出现?干杯

回答

0

preLoad功能应该是静态

public static function preLoad($template, $page) { 
0

预载功能也不是一成不变的。 ti应该看起来像这样:

public static function preLoad($template, $page) { 
     ob_start(); 

     include(VIEWS . "{$template}.php"); 

     $buffer = ob_get_contents(); 
     @ob_end_clean(); 
     return $buffer; 
    } 
2

那么preLoad函数不是静态的。这意味着只有类模板的一个对象可以使用这种方法。静态方法独立存在于该类的任何对象中。

Template :: preLoad是一个静态调用:你没有创建一个Template对象,然后调用preLoad方法。所以基本上,你有两种解决方案:

  • 使preLoad static;
  • 创建一个Template对象,然后调用它的preLoad函数。
+0

+1提供了一个很好的解释+替代解决方案 – Capsule 2011-03-01 16:09:01

0

像大家所说,你作为一个静态方法调用的函数:

Template::preLoad(xxx)

::意味着在PHP中静态的。通常将函数称为静态::或对象->调用。

函数定义为一种或另一种:

public static function preLoad($template, $page)

调用等:Template::preLoad('admin/courses/index', $this->data);

OR

public function preLoad($template, $page)

调用等Template->preLoad('admin/courses/index', $this->data);

作为参考,可以在不实例化对象的情况下调用静态函数。如果你的函数不需要运行一个对象,你可以使它静态。基本上,这意味着你不能在静态方法中引用$this。它将与给定的输入一起运行,而不必构建对象。