Yii Framework Forum: init() or __contruct() - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

init() or __contruct() Which would you say? Rate Topic: -----

#41 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 26 August 2009 - 02:41 PM

View Postqiang, on 25 August 2009 - 04:49 PM, said:

Factory is not good when subclassing is needed.

I did a Google search to find out factory drawbacks, but did not succeed. Please invest a few minutes to clarify what you meant by 'not good'.

I'm willing to accept your decision without further questions if it is well reasoned. However, I can't see any purpose why we should keep the existing instantiation process without considering alternatives.

If a factory method not only creates a new class but also configures it from an argument, you then have the ability to customize the configuration process. It also means that external classes know less about the components' internals, which is always good, even if subclassing a component takes a bit longer.
0

#42 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 26 August 2009 - 02:47 PM

Here's an example of what I thought:
class CComponent
{
   protected function __construct()
   {
      // pre-initialization
   }

   public static function factory($config=null)
   {
      $class = new self;
      // do whatever configuration needs
      // a foreach might do so far

      // post init, or init as in current implementation
      return $class;
   }
}


Edit:
The only drawback I found (bug 30934):

Quote

Special keyword 'self' represents the parent class instead of the child
class when it is used in an inherited method of the parent class.

This post has been edited by pestaa: 26 August 2009 - 02:50 PM

0

#43 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 August 2009 - 03:24 PM

Using the factory method, how would you write a child class that needs to do some initialization before parent's initialization?

The "self" bug you mentioned is a big one. This can only be solved in PHP 5.3.
0

#44 User is offline   auxbuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 164
  • Joined: 14-July 09
  • Location:Malvern, UK

Posted 26 August 2009 - 03:27 PM

View Postqiang, on 26 August 2009 - 02:36 PM, said:

I am open to your suggestions. But, please, give some concrete ones. Having typed in so many words and spent so much time, I have yet to see any from you yet (pestaa gave one, but it's not convincing enough to make the change). That's why I call for stopping discussing on this.

I needed to investigate first. Then experiment. I don't know your code, and there are no tests, so it takes a lot more time than a well organised project. So I have to create tests, branch, and validate my ideas. That's why I asked questions, to speed things along. I have no wish to rock the boat.
0

#45 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 August 2009 - 03:31 PM

No problem. There are some unit tests under "tests" folder. They only cover part of the code. Adding more coverage of tests is one of the major tasks looking forward (mainly need to find good ways to set up different fixtures).
0

#46 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 August 2009 - 03:43 PM

I apologize if my posts hurt you. I guess I am overheated.

This framework is far from perfect. There are many places that need to be improved. One of the biggest obstacles to these improvements is to maintain backward compatibility (BC). For this reason, I have to defend hard when a design change may break BC.

Having said that, I don't mean we should sit on mistakes. We may take your suggestions when we do a major release or rewrite that doesn't have keeping BC as top priority.
0

#47 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 26 August 2009 - 03:45 PM

View Postqiang, on 26 August 2009 - 03:24 PM, said:

Using the factory method, how would you write a child class that needs to do some initialization before parent's initialization?

By extending constructor and calling parent implementation lastly.

Update:
Backward compatibility is definitely an important point of view, but as these ideas are really long term, I think they simply cannot be implemented before the next major version. But it's fine.

This post has been edited by pestaa: 26 August 2009 - 03:48 PM

0

#48 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 August 2009 - 03:50 PM

View Postpestaa, on 26 August 2009 - 03:45 PM, said:

By extending constructor and calling parent implementation lastly.

Update:
Backward compatibility is definitely an important point of view, but as these ideas are really long term, I think they simply cannot be implemented before the next major version. But it's fine.


Could you write some pseudo code to show this? Note that these initialization (both parent's and child's) need to be done after configuration.
0

#49 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 26 August 2009 - 04:21 PM

Of course. Please note that prior to 5.3, PHP forces me to copy self-referential methods, which might turn the whole thing even more harmful than the current implementation. I can't decide, though. I'm also not aware the possible workaround you mentioned regarding 5.3.

class CParent
{
   protected function __construct()
   {
      // before config
   }

   public function factory($config=null)
   {
      $class = new self;
      // config
      // ...
      return $class;
   }
}

class CChild extends CParent
{
   protected function __construct()
   {
      // before parent init
      parent::__construct();
      // after parent init
   }

   public function factory($config=null)
   {
      $class=new self;
      // config
      // ...
      return $class;
   }
}

Subclasses of CActiveRecord are required to have a model function to overcome this self issue. I know this is overwhelming.

If configuration is complex, we can dedicate a protected method for it. Otherwise it can be done in the constructor (which won't make that 'a big black box'). So only factory($config=null){return new self($config);} would be necessary in each descendant.
0

#50 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 August 2009 - 04:31 PM

Nope, your code doesn't meet the requirement. I was saying the init code is needed after the configuration is applied (e.g. establishing DB connection after the connection parameters are set).
0

#51 User is offline   auxbuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 164
  • Joined: 14-July 09
  • Location:Malvern, UK

Posted 26 August 2009 - 04:40 PM

View Postqiang, on 26 August 2009 - 03:43 PM, said:

I apologize if my posts hurt you. I guess I am overheated.

I'm not at all offended. No pasa de nada.

Quote

This framework is far from perfect. There are many places that need to be improved. One of the biggest obstacles to these improvements is to maintain backward compatibility (BC). For this reason, I have to defend hard when a design change may break BC.

Sure. Completely understood.

BC (and regresion, of course) was also one of the issues I was considering, which is why I needed good test coverage of the areas I was looking at. That's not a ten minute job!

Quote

Having said that, I don't mean we should sit on mistakes. We may take your suggestions when we do a major release or rewrite that doesn't have keeping BC as top priority.

No problem,
0

#52 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 21 March 2013 - 10:20 AM

w000000t, I was so happy ready this, and all of the sudden it stops? I deserve a conclusion!
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#53 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 598
  • Joined: 27-December 10
  • Location:china

Posted 21 March 2013 - 11:42 AM

View PostAsgaroth, on 21 March 2013 - 10:20 AM, said:

w000000t, I was so happy ready this, and all of the sudden it stops? I deserve a conclusion!

:lol: so what 's your conclusion ?
0

#54 User is offline   Stratigos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 14-September 11
  • Location:BROOKLYN

Posted 26 March 2013 - 04:01 PM

Found a great use for having both __construct() and init() for CPortlet. I have a javascript library that I need to add to all pages on a client's site, and for one view, a property of this CPortlet has a different specified value than any other view. I also have a need to override this value under certain conditions.

The class that Ive extended CPortlet has a property initialized to FALSE. During its __construct(), Ive added some logic to determine if the portlet is called from one particular controller/action, and change the value of this property to TRUE. When the widget is called within my viewfile via:

<?php $this->widget('MyPortlet'); ?>


I can choose to override this property, since the logic discussed above is performed in __construct(), and more specifically, not done in init() :

<?php $this->widget('MyPortlet', array('myProperty' => TRUE)); ?>


I rarely will configure "MyPortlet" with that config, however, and prefer to just let the constructor logic determine the necessary conditions for changing 'myProperty' from FALSE to TRUE.

Im aware there are many ways to do this same thing, but I feel this is a tidy way to accomplish what I need.
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • 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