1

I have a Class like this

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

....................
....

Another class is an non-static class. Now i want to load the non-static class in my static class. I my View class i want use the functions from my Login class.

Thats an templating class and i have a function in the View class. In this function i load an defined controller (xxx.php) and in this file i want to use all classes thsts exist .

protected static function get_controller($file){
    $ctrlFile = self::$pathCtrl . $file.'.php'; 
    !file_exists($ctrlFile) || require_once($ctrlFile);

}

In the file thats included from the function i have this code.

if($login->user()){ echo "Hallo ich bin eingeloggt"; }

The error that coms in browser

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

How can I do that?

Memurame
  • 53
  • 2
  • 10

2 Answers2

0

You should define $pathCtrl and other properties as protected, because private prevents access to the property/field in the extended classes.

Additional, I don't think is a good practice (I may be wrong) to extend a static class from a non-static class, think they serve different purposes.

I use Static Classes for services and providers, Non-Static for objects.

More references to this question: What is the difference between public, private, and protected?

Community
  • 1
  • 1
0

You can't use this in a static function. Instead, try creating a new object within the static function:

$object = new Login();
$object->login();
.........

Or you could change $login to be static and then use self:

self::$login = new Login();
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77