NYCPHP Meetup

NYPHP.org

[nycphp-talk] Two objects of the same type in same script

Allen Shaw ashaw at polymerdb.org
Wed Jul 25 14:24:59 EDT 2007


David Krings wrote:
> ...I will need three editor
> components on the page and now wonder on how to add a second editor 
> object. ...
> 
> Currently, I got this:
>      $oFCKeditor = new FCKeditor('sbtext');
> ...
> When I now want to initiate a new editor, do I change it like this:
>      $oFCKeditor = new FCKeditor('sbnotes') ;
> or like this
>      $oFCKeditorNotes = new FCKeditor('sbnotes');
> 
> So, unrelated to the editor, how do I instantiate a second instance of 
> an object?
> 

Your second choice is the one you want.  The code "new 
FCKeditor('whatever')" returns a distinct FCKeditor object, which you 
should store in a distinct variable.  If you just use the same variable 
as for the first object, $oFCKeditor, you'll overwrite the value of that 
variable and lose the object.

OO concepts can be daunting at times, but in this case it's a simple 
matter of storing someting in a variable.  If you wrote:

	$fruit = 'apple';  // I need some fruit
	$fruit = 'pear';   // I need another kind of fruit

then you'd expect the 'apple' value to be overwritten and gone.  Same 
with the objects.

Hope this helps.

- Allen


-- 
Allen Shaw
slidePresenter (http://slides.sourceforge.net)



More information about the talk mailing list