Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #2165

Re: An overloading question

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: An overloading question
Date 2011-06-14 19:12 -0400
Organization A noiseless patient Spider
Message-ID <it8ps7$su3$1@dont-email.me> (permalink)
References <it83e6$u4e$1@dont-email.me> <4df7a92f$0$12468$bed64819@gradwell.net> <it8n87$7rj$1@dont-email.me>

Show all headers | View raw


On 6/14/2011 6:27 PM, sheldonlg wrote:
> On 6/14/2011 2:32 PM, Denis McMahon wrote:
>> On Tue, 14 Jun 2011 12:49:11 -0400, sheldonlg wrote:
>>
>>> I have a problem that I am wrestling with that should be so obvious and
>>> easy -- but it isn't. I have a base class that has a method that calls
>>> another method in the base class with $this->thatOtherMethod. I have
>>> another class that extends the base class and I want to have
>>> thatOtherMethod in the extended class override the one in the base
>>> class. The calling method in the base class is called using and
>>> instance of the extended class pointing to that method.
>>>
>>> Simply, here is an example:
>>>
>>> A.class.php
>>> --------------------------
>>> <?php
>>> abstract class A {
>>> public function b() {
>>> $this->a();
>>> }
>>> protected function a() {
>>> print 'In class A';
>>> }
>>> }
>>> ?>
>>>
>>> B.class.php
>>> ---------------------------
>>> <?php
>>> include_once 'A.class.php';
>>> class B extends A {
>>> public function a() {
>>> print 'In class B';
>>> }
>>> }
>>> ?>
>>>
>>> c.php
>>> ----------------------------
>>> <?php
>>> include 'B.class.php';
>>> $x = new B();
>>> $x->b();
>>> ?>
>>> ----------------------------
>>>
>>> I want it to print out "In class B", but it prints out "In class A".
>>>
>>> I have looked over Google quite a bit and found nothing that helped. I
>>> have tried making the methods public, protected, keep them both the same
>>> access, etc. and have had no luck. Suggestions?
>>
>> I think A->a() is being used as a constructor for the base class when you
>> instantiate the extended class.
>>
>> This might (or might not) be causing confusion as to what's happening.
>>
>> This code:
>>
>> <?php
>>
>> abstract class A {
>>
>> public function x() {
>> print "x() In class A\n";
>> $this->y();
>> }
>>
>> public function y() {
>> print "y() In class A\n";
>> }
>>
>> public function a() {
>> print "a() In class A\n";
>> }
>>
>> }
>>
>> class B extends A {
>>
>> public function y() {
>> print "y() In class B\n";
>> }
>>
>> }
>>
>> print "instantiate\n";
>>
>> $x = new B();
>>
>> print "call function\n";
>>
>> $x->x();
>>
>> ?>
>>
>> gives:
>>
>> instantiate
>> a() In class A
>> call function
>> x() In class A
>> y() In class B
>>
>> Not sure, though, if that solves your problem or not?
>>
>> Rgds
>>
>> Denis McMahon
>
> I modified B to give it an explicit constructor. It still didn't work.
> Why did your example print out that last line. It was never called. Why
> did it use it as a constructor? That doesn't seem to make sense.
>

Unfortunately, PHP doesn't really implement OO constructs very well - in 
fact they to a pretty piss poor job of it.  One of the problems is with 
virtual functions, as you found.  A good OO language would work like you 
want, but PHP doesn't - the constructor for A will, in this case, only 
call a function in A.  In C++, Smalltalk, Java and most other OO 
languages, you could define y() as virtual and the constructor for A 
would call the function y() in B.

But if you created a constructor in B properly, it should call B:y(). 
I'd suggest checking your code again - you might have made a mistake.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

An overloading question sheldonlg <sheldonlg@thevillages.net> - 2011-06-14 12:49 -0400
  Re: An overloading question Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-14 18:32 +0000
    Re: An overloading question sheldonlg <sheldonlg@thevillages.net> - 2011-06-14 18:27 -0400
      Re: An overloading question Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-14 19:12 -0400
        Re: An overloading question sheldonlg <sheldonlg@thevillages.net> - 2011-06-14 19:43 -0400
          Re: An overloading question Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-14 20:45 -0400
            Re: An overloading question sheldonlg <sheldonlg@thevillages.net> - 2011-06-14 21:58 -0400
              Re: An overloading question Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-14 22:08 -0400
              Re: An overloading question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-06-15 14:21 +0200
              Re: An overloading question sheldonlg <sheldonlg@thevillages.net> - 2011-06-15 12:55 -0400
        Re: An overloading question Michael Fesser <netizen@gmx.de> - 2011-06-15 18:04 +0200
          Re: An overloading question Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-15 12:53 -0400
      Re: An overloading question Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-15 03:41 +0000
      Re: An overloading question Michael Fesser <netizen@gmx.de> - 2011-06-15 18:02 +0200
  Re: An overloading question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-06-15 14:01 +0200

csiph-web