Parent class in namespace access child class variables

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:


<?php namespace Lifeframe\\Controller;

function Render ( ... )
{
  //...code...
}

I’m pretty sure the same applies without namespaces.

As for static method being OOP. Not really, what you have is functionally identical to:


function controller_render($file) {
	global $name, $vars;
	extract($vars, EXTR_OVERWRITE);
        include ROOT_PATH.DS.'views'.DS.$name.DS.$file.'.tpl';
}

That worked perfect, thanks.

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?

Ah the bane of static methods.

The problem is the way self:: is resolved.

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.