For Programmers: Free Programming Magazines  


Home > Archive > PHP PEAR Questions and Answers > April 2004 > Re: [PEAR-QA] assign to $this









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR-QA] assign to $this
Alan Knowles

2004-04-15, 9:37 am

> $pager =& Pager::factory($options);

I'd rather see
$pager = Pager::construct($options);

as factory normally indicates that it may return a implementation of
Pager, (eg. like an inherited class)


I'm not sure if removing things like returning PEAR_Error from
constructors would be called BC break.. - more of a major bug fix..

i know I have _never_ tested return values from new Class...

I would suggest:

modifying the constructor - and add the following..
(for the time being support existing behaviour, but trigger an E_WARNING)


if (func_get_args()) {
if (php_version() > 4 ) {
echo "Use ::construct()";
exit;
}
trigger_error("Constructor is depreciated",
"- use ::construct",E_WARNING);
}


Regards
Alan

>
> Thanks for your attention,
> Lorenzo
>



--
Can you help out?
Need Consulting Services or Know of a Job?
http://www.akbkhome.com
Lorenzo Alberton

2004-04-15, 9:38 am

On Thu, 15 Apr 2004 20:56:43 +0800, Alan Knowles wrote:
>
> I'd rather see
> $pager =3D Pager::construct($options);
>
> as factory normally indicates that it may return a implementation
> of Pager, (eg. like an inherited class)


er, actually it's very close to that:
Pager v.2.x is only a delegate for Pager_Jumping
and Pager_Sliding, as you can see from Pager.php source.

So I guess a factory() method is more proper than a constructor.


> I would suggest:
> modifying the constructor - and add the following..
> (for the time being support existing behaviour, but trigger an
> E_WARNING)
>
> if (func_get_args()) {
> if (php_version() > 4 ) {
> echo "Use ::construct()";
> exit;
> }
> trigger_error("Constructor is depreciated",
> "- use ::construct",E_WARNING);
> }



Yep, that's something I wanted to do to help with
the transition. That's just a warning to inform about
the unavoidable BC break, though... :-)


Thanks,
Lorenzo
Lukas Smith

2004-04-15, 1:32 pm

Lorenzo Alberton wrote:
> Hi guys,
>
> this will probably start another long thread
> (I hope not, though, since I'm still catching
> up with the long Easter ones...).
>
> I think it's time to face the "assign to $this" problem.
>
> I'm focusing on PEAR::Pager now, but the same
> applies to these packages, maybe others as well:
>
> http://pear.php.net/bugs/search.php...lay&status=Open
>
> The solution proposed by Toby in the QA reports
> won't work, as php5 will die as soon as it *sees*
> an assignment to $this, even if it never encounters
> it in its workflow, so "if()" checks are out of the game.


just as an FYI eval() would work as Derick pointed out to me.

regards,
Lukas Smith
smith@backendmedia.com
_______________________________
BackendMedia
www.backendmedia.com
berlin@backendmedia.com

Linn Zwoch Smith GbR
Pariser Str. 44
D-10707 Berlin

Tel +49 30 83 22 50 00
Fax +49 30 83 22 50 07
Lorenzo Alberton

2004-04-15, 6:32 pm

On Thu, 15 Apr 2004 18:35:41 +0200, Lukas Smith wrote:
>
> just as an FYI eval() would work as Derick pointed out to me.


Ok, I confirm that. The eval() solution [1] works :-)

Just a quick note:

The PEAR-QA proposed solution had the following "if" clause:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3
D
if (version_compare(phpversion(), '5.0.0') =3D=3D -1)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3
D

this check evaluates to true on 5.0.0RC, though,
so I propose another one:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3
D
if (get_class($this) =3D=3D 'lower_case_class_name') { //php4 lowers class=
names
// assign factoried method to $this for PHP 4
eval('$this =3D <ClassName>::factory($options);');
} else { //php5 is case sensitive
$msg =3D 'In PHP5 you must use the "<ClassName>::factory($params)"=
method';
trigger_error($msg, E_USER_WARNING);
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3
D

it should work.

HTH
Lorenzo

[1] http://marc.theaimsgroup.com/?l=3Dp...684332227&w=3D2
Tobias Schlitt

2004-04-15, 7:32 pm

Lorenzo Alberton wrote:

>Ok, I confirm that. The eval() solution [1] works :-)
>

But do we really want to recommend eval()?
No objection from my side, but it should really be pointed out, that
this is a emergency <http://dict.leo.org/?p=14/p..&search=emergency>
solution <http://dict.leo.org/?p=14/p..&search=solution> and should not
become common.

--
Tobias Schlitt GPG Key: 0xA6529579
a passion for php http://www.schlitt.info
Lorenzo Alberton

2004-04-15, 7:32 pm

On Fri, 16 Apr 2004 00:07:46 +0200, Tobias Schlitt wrote:
> Lorenzo Alberton wrote:
> But do we really want to recommend eval()?
> No objection from my side, but it should really be pointed out,
> that this is a emergency solution and should not
> become common.


side-quoting Lukas from a php-internals post,
it's an ugly hack for an ugly issue.

It's the only (and I repeat "only") working solution I've heard
so far for this problem.
I'm not reccomending it if not absolutely necessary, but
even if it's the first time I'm using eval(), I don't consider this
function evil (at least for this fairly safe case).
Many pear packages are using it, btw...

So, if a class is internally (as in "deep inside") using an assignment
to $this, probably there are other means to keep BC.
If its usage is exposed like in a constructor, then I fear
there's not much else to do.

Best regards,
Lorenzo
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com