PHP CLass 静态和非静态
PHP CLass static & non static
我有一个Class这样的
class View
{
// Parameter die an das template übergeben werden
protected static $params = Array();
// Parameter die vom Routing übergeben werden
public static $routeParams;
// haupttemplate
protected static $viewMainContent;
// view template
protected static $viewFileContent;
// view pfad
protected static $pathTpl;
// controller pfad
protected static $pathCtrl;
protected $login;
// ausgabe des templates
public static function get($view, $params = "", $master = "main"){
$this->$login = new Login();
self::$pathTpl = Config::get('SRVROOT') . '/views/';
self::$pathCtrl = Config::get('SRVROOT') . '/controller/';
self::$routeParams = $params;
// prüfen ob main template oder custom
....................
....
另一个class是一个非静态的class。
现在我想在我的静态 class 中加载非静态 class。
我的视图 class 我想使用登录 class 中的功能。
那是一个模板 class,我在视图中有一个函数 class。
在这个函数中我加载了一个定义的控制器(xxx.php)并且在这个文件中我想使用所有 classes thsts exist .
protected static function get_controller($file){
$ctrlFile = self::$pathCtrl . $file.'.php';
!file_exists($ctrlFile) || require_once($ctrlFile);
}
在函数包含的文件中我有这段代码。
if($login->user()){ echo "Hallo ich bin eingeloggt"; }
浏览器报错
Fatal error: Uncaught Error: Using $this when not in object context in /home/vagrant/Cloud/60_Projekte/SeitenVorlage/lib/class.templating.php on line 25
我该怎么做?
您应该将 $pathCtrl 和其他属性定义为 protected,因为 private 阻止访问扩展 classes 中的 property/field。
此外,我认为从非静态 class 扩展静态 class 不是一个好的做法(我可能是错的),认为它们有不同的用途。
我对服务和提供者使用静态 类,对对象使用非静态。
更多参考这个问题:What is the difference between public, private, and protected?
您不能在静态函数中使用 this
。相反,尝试在静态函数中创建一个新对象:
$object = new Login();
$object->login();
.........
或者您可以将 $login 更改为静态的,然后使用 self
:
self::$login = new Login();
我有一个Class这样的
class View
{
// Parameter die an das template übergeben werden
protected static $params = Array();
// Parameter die vom Routing übergeben werden
public static $routeParams;
// haupttemplate
protected static $viewMainContent;
// view template
protected static $viewFileContent;
// view pfad
protected static $pathTpl;
// controller pfad
protected static $pathCtrl;
protected $login;
// ausgabe des templates
public static function get($view, $params = "", $master = "main"){
$this->$login = new Login();
self::$pathTpl = Config::get('SRVROOT') . '/views/';
self::$pathCtrl = Config::get('SRVROOT') . '/controller/';
self::$routeParams = $params;
// prüfen ob main template oder custom
....................
....
另一个class是一个非静态的class。 现在我想在我的静态 class 中加载非静态 class。 我的视图 class 我想使用登录 class 中的功能。
那是一个模板 class,我在视图中有一个函数 class。 在这个函数中我加载了一个定义的控制器(xxx.php)并且在这个文件中我想使用所有 classes thsts exist .
protected static function get_controller($file){
$ctrlFile = self::$pathCtrl . $file.'.php';
!file_exists($ctrlFile) || require_once($ctrlFile);
}
在函数包含的文件中我有这段代码。
if($login->user()){ echo "Hallo ich bin eingeloggt"; }
浏览器报错
Fatal error: Uncaught Error: Using $this when not in object context in /home/vagrant/Cloud/60_Projekte/SeitenVorlage/lib/class.templating.php on line 25
我该怎么做?
您应该将 $pathCtrl 和其他属性定义为 protected,因为 private 阻止访问扩展 classes 中的 property/field。
此外,我认为从非静态 class 扩展静态 class 不是一个好的做法(我可能是错的),认为它们有不同的用途。
我对服务和提供者使用静态 类,对对象使用非静态。
更多参考这个问题:What is the difference between public, private, and protected?
您不能在静态函数中使用 this
。相反,尝试在静态函数中创建一个新对象:
$object = new Login();
$object->login();
.........
或者您可以将 $login 更改为静态的,然后使用 self
:
self::$login = new Login();