-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to instantiate the called class instead of the CActiveRecord class #4532
Conversation
The problem of this implementation is that this method needs to copied onto the children class to be instantiated correctly, since if the method is inherited __CLASS__ will always point to CActiveRecord instead of the class calling this method This change allows that if class A extends CActiveRecord {} And we call A::model(), we will get an A instance instead of CActiveRecord <?php class B { public static function model($className = __CLASS__) { return new $className(null); } } class A extends B { } $b = new B(); $a = new A(); var_dump($b::model()); var_dump($a::model()); returns object(B)yiisoft#3 (0) { } object(B)yiisoft#3 (0) { } but if we change that to get_called_class() class B { public static function model($className = null) { if($className === null){ $className = get_called_class(); } return new $className(null); } } class A extends B { } $b = new B(); $a = new A(); var_dump($b::model()); var_dump($a::model()); results in object(B)yiisoft#3 (0) { } object(A)yiisoft#3 (0) { }
PR Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should change, we've already accepted this in Yii1, we're all used to it, makes no sens to change now. |
you mean, having to clone the model method in every model, to get the correct instance? I'm sure you everybody has done this in all their yii1 projects, this change, should be backwards compatible with those cases. @twisted1919 what's your use case for this function? Btw: I don't want to start a war, since this can be fixed in user code, you can extend the CActiveRecord class and have this method there and use that to extend your model. |
Yeah, everybody had the copy of the method already because that's what Gii generates, that was my point, it is really a non-problem, all the models already have that when they are generated by Gii with the default template. What stops you to extend CActiveRecord, implement your change in that class, then extend your models from it? |
I just extended CActiveRecord as I said earlier and for some model classes I do have some custom code, but in general it's just overriding the parent implementation to get the right model class, nothing too fancy. In every model, I know, lots of repetition, but i've done it so many times, I simply ignore it now. Just to be on the same page, I have nothing against your change per-se, I just think we're already way too used to have that method in each model that at this point it doesn't matter that much to me if the ergonomics are improved. |
@twisted1919 you're totally right, my reasoning is I don't see the point to keep it that way, having that code added to every model is pointless (literally), because the finality of the model() method is to get the instance of your model, not the CActiveRecord, it appears this comes from the days of yii 1.0, when get_called_class() did not exist and I'm not sure if there was a workaround around this back then (prior to php 5.3) I would extend the scope of this, updating the model template and getting rid of the model method better late than never PS: Thanks @twisted1919 for giving your point of view |
plus updating the docs, I think the use case is heavy documented, I might be wrong though. I defer to @marcovtwout after all, whatever he thinks. It doesn't change things for me as long as there's no BC, if it makes your life easier, sure. |
This is not a fully BC change. |
True PS: I've set, "Breaks BC" to Yes in the top post |
It's a nice idea, but for Yii 1 it wouldn't make sense anymore since the current implementation is well established and documented. Changing this now, even in a backward compatible manner, would only cause confusion. |
The problem of this implementation is that this method needs to copied onto the children class to be instantiated correctly, since if the method is inherited _ _ CLASS _ _ will always point to CActiveRecord instead of the class calling this method, using get_called_class() fixes that, this function is present since php 5.3.0
Here's an example:
class A extends CActiveRecord {}
And we call A::model(), we will get an A instance instead of CActiveRecord
returns
but if we change that to get_called_class()
results in