CModel::afterConstruct() is never run

I have a model extending CModel. I want to initialize this model inside protected function afterConstruct(), but that function is never run!

Also there is no constructor in any of the parent classes that initiates the afterConstructor() or onAfterConstructor() functions. So they will never run. Is this a bug?

Example code:

class MyModel extends CModel {

    public $name;

    // required for extending CModel
    public function attributeNames() {
        return array('name');

    public function __construct() {
        // Uncomment this next line, and you get an error saying "cannot call constructor":

    // This is never run
    //public function afterConstruct() {
    protected function afterConstruct() {
        echo "inside afterConstruct()";

    // This is never run
    public function onAfterConstruct($event) {
        echo "inside onAfterConstruct()";

I am running Yii 1.1.24 (last version)

Try implementing the abstract “procedure” with the correct signature (protected void).
(not tested)

Changed the function to protected, that does not work either.

Please note that none of the parent classes implement the contructor, so the method afterConstruct() will never be called anywhere (it seems).

I might post an issue if I am right, or if there isn’t any good explanation for this.

I think you have to implement the constructor if you use your own CModel child class.
See how CFormModel implements a constructor

Perhaps you should use CFormModel as your base class.