Re: Inheritance and a class function: on what class was it called?
| From: | Nick Eby | Date: | Wed, 02 Oct 2002 18:53:26 +0000 |
| Subject: | Re: Inheritance and a class function: on what class was it called? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-118635@lists.php.net to get a copy of this message | ||
when you call a function with ->, you must call it on an object ($c->foo(),
for example). the function is termed an "object function" when used that
way. php has an operator :: that enables that same function to be called
without a specific object; in this case, I'm referring to it as a "class"
function. another term for this is usually "static" function. any function
of a class can be called in both ways. the only catch is that, when called
as a class function, the function can't in turn call any functions on $this
or a fatal error will result.
sorry this is so confusing, I probably should've used the term "static" from
the beginning.
/nick
----- Original Message -----
From: "Debbie Dyer" <debbie.dyer@c-u-online.co.uk>
To: "Nick Eby" <nick_eby@bonzidev.com>
Cc: <php-general@lists.php.net>
Sent: Wednesday, October 02, 2002 11:33 AM
Subject: Re: [PHP] Inheritance and a class function: on what class was it
called?
> I dont understand what you are saying then or what you are trying to do.
>
> How can you call C::foo() outside of a class? You access class functions
> externally with ->
>
> Debbie
>
> ----- Original Message -----
> From: "Nick Eby" <nick_eby@bonzidev.com>
> To: <php-general@lists.php.net>
> Sent: Wednesday, October 02, 2002 7:24 PM
> Subject: Re: [PHP] Inheritance and a class function: on what class was it
> called?
>
>
> > not quite... the case I'm interested in is this, given your example:
> >
> > print C::foo();
> >
> > which would print nothing, unfortunately, since when the function is
> called
> > using the :: operator there is no $this variable. basically what I'm
> > looking for is a replacement for get_class($this) when the function is
> > called using ::
> >
> > thanks
> >
> > /nick
> >
> > "Debbie Dyer" <debbie.dyer@c-u-online.co.uk> wrote in message
> > news:054c01c26a3f$34f6d230$de093c3e@homepc...
> > > I think I know what you mean. You could get it to return the name of
the
> > > class.
> > >
> > > eg.
> > >
> > > class A {
> > > function foo () {
> > > return get_class($this);
> > > }
> > > }
> > > class B extends A {
> > > }
> > > class C extends A {
> > > }
> > >
> > > $a = new A();
> > > $b = new B();
> > > $c = new C();
> > > print $a->foo();
> > > print $b->foo();
> > > print $c->foo();
> > >
> > > Is this what you mean?
> > >
> > > Debbie
> > >
> > > ----- Original Message -----
> > > From: "Nick Eby" <nick_eby@bonzidev.com>
> > > To: <php-general@lists.php.net>
> > > Sent: Wednesday, October 02, 2002 6:29 PM
> > > Subject: [PHP] Inheritance and a class function: on what class was it
> > > called?
> > >
> > >
> > > > Assume you've got some class that has one or more classes inherited
> from
> > > it.
> > > > The parent class has a function that is normally called using the ::
> > > > operator (a class function). Assume also that the class function is
> > never
> > > > called from an object function. Is it possible to find if the class
> > > > function was called on the parent class, or on the child class?
> > > >
> > > > example:
> > > > Class A has a function foo(), callable by A::foo(). Class B extends
A.
> > If
> > > > outside code makes the call B::foo(), can you tell from within foo()
> > that
> > > > the function was called on B and not A?
> > > >
> > > > I think this is impossible but I want to confirm that suspicion.
It's
> > > > important that the call B::foo() isn't made from inside any object
> > > function,
> > > > so that there's no context for the $this variable; the get_class()
> > > function
> > > > isn't applicable.
> > > >
> > > >
> > > >
> > > > --
> > > > 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
> >
> >