Yii Framework Forum: Why "private" scope? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Why "private" scope? Guys, why do you use "private" scope in Yii classes? Rate Topic: -----

#1 User is offline   Sw00p 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-January 12

Posted 09 January 2012 - 07:58 AM

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
0

#2 User is offline   ololo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 23-January 11
  • Location:Minsk, Belarus

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
0

#3 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 09 January 2012 - 02:44 PM

View Postololo, 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
0

#4 User is offline   Sw00p 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-January 12

Posted 01 February 2012 - 05:39 PM

Thank you guys! It's right and usefull thoughts, but there is code like
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?
0

#5 User is offline   Weavora Team 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 06-December 10

Posted 02 February 2012 - 01:32 AM

View Postandy_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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users