364,088 Members | 5404 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Php 5 : polymorphic class

Baptiste Pillot
P: n/a
Baptiste Pillot
Hello,


With php4 I did the following in class initialisation :

class base_class {

function modify_polymorphic($otherclass){
$this=new $otherclass();
}

function other_function(){
echo "Base method";
}

}

class herited_class extends base_class {

function other_function(){
echo "Herited method";
}

}

$base=new base_class();
$base->modify_polymorphic("herited_class");
$base->other_function();

This will echo "Herited method" in PHP 4.
But in PHP 5 it does not wand to work at all.
PHP interpreter says :

Fatal error: Cannot re-assign $this in scriptname.php on line 10
Oct 10 '05 #1
Share this Question
Share on Google+
4 Replies


Jerry Stuckle
P: n/a
Jerry Stuckle
Baptiste Pillot wrote:[color=blue]
> Hello,
>
>
> With php4 I did the following in class initialisation :
>
> class base_class {
>
> function modify_polymorphic($otherclass){
> $this=new $otherclass();
> }
>
> function other_function(){
> echo "Base method";
> }
>
> }
>
> class herited_class extends base_class {
>
> function other_function(){
> echo "Herited method";
> }
>
> }
>
> $base=new base_class();
> $base->modify_polymorphic("herited_class");
> $base->other_function();
>
> This will echo "Herited method" in PHP 4.
> But in PHP 5 it does not wand to work at all.
> PHP interpreter says :
>
> Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]

That is correct. It's never been valid to reassign $this. But previous
versions of PHP let it by.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Oct 10 '05 #2

Mladen Gogala
P: n/a
Mladen Gogala
On Mon, 10 Oct 2005 20:24:51 +0200, Baptiste Pillot wrote:
[color=blue]
> Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]

$this is something that points to current object. Reassigning $this is
like flying by pulling your hair. Ain't possible.

--
http://www.mgogala.com

Oct 11 '05 #3

Tony Marston
P: n/a
Tony Marston

"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:SbudndpdiOjQJNfeRVn-iA@comcast.com...[color=blue]
> Baptiste Pillot wrote:[color=green]
>> Hello,
>>
>>
>> With php4 I did the following in class initialisation :
>>
>> class base_class {
>>
>> function modify_polymorphic($otherclass){
>> $this=new $otherclass();
>> }
>>
>> function other_function(){
>> echo "Base method";
>> }
>>
>> }
>>
>> class herited_class extends base_class {
>>
>> function other_function(){
>> echo "Herited method";
>> }
>>
>> }
>>
>> $base=new base_class();
>> $base->modify_polymorphic("herited_class");
>> $base->other_function();
>>
>> This will echo "Herited method" in PHP 4.
>> But in PHP 5 it does not wand to work at all.
>> PHP interpreter says :
>>
>> Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]
>
> That is correct. It's never been valid to reassign $this. But previous
> versions of PHP let it by.[/color]

You sample code also shows that you do not understand what "polymorphism"
actually means.

--
Tony Marston

http://www.tonymarston.net



Oct 16 '05 #4

Jerry Stuckle
P: n/a
Jerry Stuckle
Tony Marston wrote:[color=blue]
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:SbudndpdiOjQJNfeRVn-iA@comcast.com...
>[color=green]
>>Baptiste Pillot wrote:
>>[color=darkred]
>>>Hello,
>>>
>>>
>>>With php4 I did the following in class initialisation :
>>>
>>>class base_class {
>>>
>>>function modify_polymorphic($otherclass){
>>> $this=new $otherclass();
>>>}
>>>
>>>function other_function(){
>>> echo "Base method";
>>>}
>>>
>>>}
>>>
>>>class herited_class extends base_class {
>>>
>>>function other_function(){
>>> echo "Herited method";
>>>}
>>>
>>>}
>>>
>>>$base=new base_class();
>>>$base->modify_polymorphic("herited_class");
>>>$base->other_function();
>>>
>>>This will echo "Herited method" in PHP 4.
>>>But in PHP 5 it does not wand to work at all.
>>>PHP interpreter says :
>>>
>>>Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]
>>
>>That is correct. It's never been valid to reassign $this. But previous
>>versions of PHP let it by.[/color]
>
>
> You sample code also shows that you do not understand what "polymorphism"
> actually means.
>[/color]

Noe my code...


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Oct 16 '05 #5

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP