473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Php 5 : polymorphic class

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
4 2500
Baptiste Pillot wrote:
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


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.
js*******@attglobal.net
==================
Oct 10 '05 #2
On Mon, 10 Oct 2005 20:24:51 +0200, Baptiste Pillot wrote:
Fatal error: Cannot re-assign $this in scriptname.php on line 10


$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

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:Sb********************@comcast.com...
Baptiste Pillot wrote:
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


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


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

--
Tony Marston

http://www.tonymarston.net

Oct 16 '05 #4
Tony Marston wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:Sb********************@comcast.com...
Baptiste Pillot wrote:
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


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

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


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dave | last post by:
What is the expected output of this program and why??? #include <iostream> using namespace std; class base {
4
by: Maurice Termeer | last post by:
Hi, suppose i've got this: class a { public: int n; }; class b : public a { public: };
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
7
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory...
9
by: Karel Miklav | last post by:
In lots of places in a programm I need to identify type of received messages, so I create them as virtual classes and use RTTI to find their type later. But these are simple messages, often without...
7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
12
by: Bob | last post by:
Hi, 'Shadowed' properties are not polymorphic. (See thread 'Inheritance and late binding') They should be. Problem: Base class has read only property 'X'. Derived class must have read / write...
4
by: Osamede.Zhang | last post by:
Compiler store a pointer table in each object with virtual fuction to implement polymorphic,isn't it? But where is the pointer table? It seems that object only store a pointer to the table,the...
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
7
by: karl | last post by:
I've got a abstract class CBase and two classes CPoly1 and CPoly2 that inherit from CBase. Is there a way to find out the classtype (CPoly1 or CPoly2) when iterating over a list of objects for...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.