I’m not sure if I’ve run into a bug here, but I’m having this issue:
<?php
namespace Liteframe;
class Controller
{
public static function render($file)
{
extract(self::$vars, EXTR_OVERWRITE);
include ROOT_PATH.DS.'views'.DS.self::$name.DS.$file.'.tpl';
}
}
Whereas the child class (should be extends \Liteframe\Controller)
class LookupController extends \\\\Liteframe\\\\Controller
{
public static $name = 'lookup';
public static function index()
{
self::render('test');
}
}
Yields the error:
Fatal error: Access to undeclared static property: Liteframe\\Controller::$name in /var/projects/lookup/php/liteframe/LF_BaseController.class.php on line 17
Which is strange that it’s not looking to the child class. Am I doing something wrong here?
I’m updating a MVC ‘package’ I use for projects to be namespaced.
Using static in classes was a workaround for the lack of namespace in previous versions of PHP.
That is the only major reason it was ever used.
You would be better off just doing:
I like the static methods, still is OOP with singleton when needed, but it works out this way nicely. self:: seems to resolve differently from without using namespaces, I guess that’s by design?
In Controller::render “self” references “Controller” so it’s doing Controller::render();
$name is obviously declared on the child class.
To get around it you need to make use of get_called_class(). However, you may want to reconsider your usage of static methods and look at an OOP approach.