What is the best way to get the child class name for use in a parent class I am currently trying to implement a very simple MVC framework as a way of helping me understand how they work. My base controller class has 2 properties, a model and a view. All controllers have a generic view class property however only certain controllers have a model. 

I needed a way of obtaining the name of the child controller class in the base controller class so that in the constructor I can check to see if a model class exists and instantiate one if so. I think I have come up with a solution using late static binding. I do not fully understand the concept, but as far as I can tell, if used in a parent class the static keyword refers to the child class.

I also implemented a function className() in the controller class which uses the 'this' keyword to get the name of the current class. This function is then called with the static keyword resulting in the name of the child class being obtained.

Below is the code for the Controller class. All models are titled controllerName_model.

/**
* Base controller class, creates a generic view class and a model class (if one exists for the child class) for the controller.
*/
class Controller
{
    public $view;
    public $model;

    public function __construct()
    {
        //Create a new genereic view object
        $this-&gt;view = new View();

        //Use late static binding to get the name of the child class extending this base class
        $name = static::className();
        //Create the path to the related model (if it exists)
        $path = 'models/' . $name . '_model.php';

        //If the model exists then require the file and create a new model object and store in $this-&gt;model
        if(file_exists($path)) {
            require $path;

            $modelName = $name . '_Model';
            $this-&gt;model = new $modelName();
        }
    }

    /**
    * This class returns the name of each class that extends this base controller class. It is used in the 
    * constructor with late static binding to get the name of the child class so that a model can be created if one exists.
    */
    public function className() {
        return get_class($this);
    }
}


As far as I can tell this method is working but I am not confident that it is correct or that it is the best way of doing this. If anyone knows of any other ways of doing this or can point out any flaws in this approach I would be extremely grateful. Thanks.
Looks fine to me; though somethings to think about. Do you need to run get_class? Or might it be better to have a model variable in the child class

class Blog extends Controller
{
    private $modelClass = 'Blog';
}


The overhead of get_class might be small, but probably more than a variable; but this way also give you the ability of being able to use the same model with multiple controllers if you need to. Granted you could just inherit with the models too, but that would mean 2 includes instead of one.

If you don't go that route, consider some validation, string tidying or consistency in your model/file names.

$path = 'models/' . $name . '_model.php';


will likely equal Blog_model.php but your class is Blog_Model see the mixing of cases; consider Blog_Model.php or doing a strtolower($name). This might seem minor, but different Operating Systems handle case differently and I have been bitten by this exact issue.

I noticed you are doing a require rather than a require_once which could lead you to have cannot re-declare class errors. if require_once isn't for you consider 

if(!class_exists($modelName))


This might seems abstract at the moment, but when I came to upgrade an admin system for a custom CMS I found it really handy for the admin Controllers to wrap the frontend wesbites controllers, and adding a little checking could greatly increase your flexibility further down the road.

If you haven't done already check out __autoload http://php.net/manual/en/language.oop5.autoload.php if might be more overhead, but it is just so handy I really miss it on legacy projects.