Hi all!
I've been using Yii as my second framework for a couple of years and I like it. But I have annoying question: why, why you guys use private scope in framework classes instead of protected?! It's craziness, it makes terrible extending of the base functionality, I have to copy your base classes to extend their functionality, not to simply extend them. Is there any explanation of this?
Sincerely,
Yuriy
Page 1 of 1
Why "private" scope? Guys, why do you use "private" scope in Yii classes?
#2
Posted 09 January 2012 - 02:03 PM
Quote
Having a few well defined extension points force the developer to extend your library the right way instead of hacking your code.
© Fabien Potencier - Pragmatism over Theory: Protected vs Private - here are some thoughts on the subject
#3
Posted 09 January 2012 - 02:44 PM
ololo, on 09 January 2012 - 02:03 PM, said:
© Fabien Potencier - Pragmatism over Theory: Protected vs Private - here are some thoughts on the subject
Hah, I just wanted to mention that article too. But actually in Yii classes there are no "too many" private methods, at least I didn't face any big problems extending core classes. And I agree making everything public/protected can make more problems for developers, because there are no guarantees these methods won't change in the future.
More thoughts on the subject: http://en.wikipedia....ver_inheritance
#4
Posted 01 February 2012 - 05:39 PM
Thank you guys! It's right and usefull thoughts, but there is code like
Directory name is hardcoded here, to use another directory name (not 'views') I should rewrite method in child class, but there are private properties... Yes, I can simply replace 'view' in result, but this is not good idea. Can you change my opinion?
public function getViewPath($checkTheme=false)
{
$className=get_class($this);
if(isset(self::$_viewPaths[$className]))
return self::$_viewPaths[$className];
else
{
if($checkTheme && ($theme=Yii::app()->getTheme())!==null)
{
$path=$theme->getViewPath().DIRECTORY_SEPARATOR;
if(strpos($className,'\\')!==false) // namespaced class
$path.=str_replace('\\','_',ltrim($className,'\\'));
else
$path.=$className;
if(is_dir($path))
return self::$_viewPaths[$className]=$path;
}
$class=new ReflectionClass($className);
return self::$_viewPaths[$className]=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'views';
}
}
Directory name is hardcoded here, to use another directory name (not 'views') I should rewrite method in child class, but there are private properties... Yes, I can simply replace 'view' in result, but this is not good idea. Can you change my opinion?
#5
Posted 02 February 2012 - 01:32 AM
andy_s, on 09 January 2012 - 02:44 PM, said:
... But actually in Yii classes there are no "too many" private methods, at least I didn't face any big problems extending core classes.
Yeah, but Yii has many private properties... That's a major issue in my practice, because for example you can't just copy-paste method if it use private properties, or extend it.. Or whatever you need...
I think just really low-level things should be encapsulated. All other should be protected. Of course some methods could be changed, but could be not. If developer call inherit protected method - he should know about the risk.
Share this topic:
Page 1 of 1

Help













