Edward Kay wrote: > Hi Richard, > > This is very strange. I can certainly reproduce the problem. Putting a > var_dump($this) in ClassA's save method shows that PHP sees $this as an > instance of ClassB - when it should be of ClassA. what *you* think it should be and what php's developers decided it should be about 3+ years ago obviously differ. if you have an instance of ClassB it is exactly that - refering to $this in the parent class definition won't magically make the class of the instance change. $this is plain this and not something else. here is a code snippet to chew on - hope it gives you a lightbulb moment: class A { function test() { self::test2(); } function test2() { echo "TEST: ",__CLASS__,"\n"; } } class B extends A { function test2() { echo "TEST: ",__CLASS__,"\n"; } } $b = new B; $b->test(); $b->test2(); > > I'm afraid it's stumped me too! Does anyone else have any thoughts? RTM - especially the php5 OO section(s) > > Edward > > >> Hi, >> >> I came across this problem and I was wondering if someone might be able >> to explain to my why it is happening. I put together a short example to >> illustrate the problem that I am having. >> >> Basically I have one class that performs a certain task. I then have a >> second class which extends the first class and performs some tasks in >> addition to the first class. Even though ClassA should know nothing >> about ClassB since ClassA is the parent, ClassA actually calls ClassB's >> exist method when it should be calling it's own. >> >> I know that description is really wordy and probably confusing which is >> why I provided below the example code that illustrates the problem. >> >> Doesn't this break the idea of inheritance? I can get it to work if I >> change ClassB::exists to some other name such as ClassB::exists2. >> >> If anyone can enlighten me as to why this problem is occurring or >> suggest a fix I would really appreciate it. Thank you. >> >> >> >> expected output >> --------------- >> ClassB - save >> ClassA - save >> ClassA - exists >> ClassA - update >> >> >> >> actual output >> ------------- >> ClassB - save >> ClassA - save >> >> >> Warning: Missing argument 2 for ClassB::exists(), called in >> /website/overridetest.php on line 6 and defined in >> /website/overridetest.php on line 33 >> >> ClassB - exists >> ClassA - update >> >> >> >> overridetest.php >> ---------------- >> <?php >> class ClassA { >> public function save($value) { >> echo "ClassA - save\n"; >> >> if ($this->exists($value)) >> $this->update($value); >> else >> $this->insert($value); >> } >> >> protected function insert($value) { >> echo "ClassA - insert\n"; >> } >> >> protected function update($value) { >> echo "ClassA - update\n"; >> } >> >> public function exists($value) { >> echo "ClassA - exists\n"; >> return true; >> } >> } >> >> class ClassB extends ClassA { >> public function save($value, $classAValue) { >> echo "ClassB - save\n"; >> parent::save($classAValue); >> // do some additional work... >> } >> >> public function exists($value, $classAValue) { >> // does a different search than ClassA >> echo "ClassB - exists\n"; >> return true; >> } >> } >> >> $c = new ClassB(); >> $c->save('test', 'test2'); >> ?> >> >> -- >> Richard Morris >> HDD Broker, Inc. >> >> Toll-Free: +1 866 960-3331 >> International: +1 250 474-6022 >> Fax: +1 250 474-6099 >> E-Mail: richard@xxxxxxxxxxxxx >> Web Site: www.hddbroker.com >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php