How to override core class in 1.1.9?

Yii 1.1.9 allows to override Core Classes, but as far as I know we have no guide nor directions to make the override. Looking at the code I found the following in YiiBase.php:

But ‘build’ is not in the default Yii downloadable package. I found some directions from MDOMBA in a post, pointing out to download BUILD from here:

I downloaded build, but it seems to have other requisites, like PHPUnit. There is a ‘core developer guide’ or something similar somewhere?

At this point I’m lost because I cant find a guide to Override the CHtml Core class.

I tried to modify the $_coreClasses array in YiiBase but nothing happens. Could anyone post clear directions to update this array in order to override a class, like CHtml in my case?

In my particular case I put this in the array:

	'CHtml' => '/../myapp/protected/components/web/helpers/XCHtml.php',

and XCHtml.php looks like this:

Thanks in advance!

PD: Would be great to override this class without modifications in files under the /framework directory…

You don’t need the build folder at all for this…

Check this part of the documentation -

Thanks mdomba, but I already seen that documentation and tried to put my CHtml replacement in Yii::$classMap, with no luck.

I tried this:


ant this:

The first try shows Fatal error: Class ‘CHtml’ not found in C:\xampp\htdocs\yii\licit\protected\components\web\helpers\XCHtml.php on line 2.

That’s OK, so I tried to include or import the original class.

The second try works, but uses the original CHtml, my XCHtml class is not used. No override is made.

The second shows this message: Fatal error: Class ‘CHtml’ not found in C:\xampp\htdocs\yii\licit\protected\components\web\helpers\XCHtml.php on line 2

So, I still lost. Any clue about the right way to override the original XHtml class with a replacement class that inherits the first?

There’s no need to change Yii::$classMap when you want to extend a framework class. Just put the file that contains your extended class in an imported directory (protected/components). Use $classMap only when you want to replace a class entirely. In this case the replacement class must have the same name as the original class (for obvious reasons) - it means that you can’t extend the replacement class from the original one.

Thank you phtamas, I understand. I finally used:

and made a full copy of the original CHtml class, replacing the desired methods.

That’s not ideal, but it works without modifying the core class.

By the way, I need to use the same class name because I want to override the behavior of CHtml in the whole application, including zii components.

Would be great to be able to inherit the original class, override the desired methods, and use the new class with a new name in the application mapping the original name to the new name. No idea on how can it be done, be would be great =)

ok I have been trying to do the same thing. You’ve gotten so close. How does one simply override only the “method” that you want to change/alter in Chtml Class. For example hiddenField() function does not allow values that are arrays. I simply want to override that method when I am building a form any thoughts on how to override specific methods not the entire class itself.

You can try this approach:

yes I have view your post a ton and it would accomplish what I need. MY orignal problem was that when I tried to strore checkbox list values in the DB they were be stored as an Array? rather than the individual values. But I do not want to highjack the thread and post elsewhere for a solution I will edit this post once I find the appropriate discussion.

ok I did not go the route of extending but created a widget to do my form previews and use stateful=true to pass POST to confirm page thanks though.