Use get_class($this)

function Go()
{
    if (!isset($this))
    {
        echo 'Called Statically<br />';
    }
   switch(get_class($this))
  { // note get_class is lowercase (which I think is a bug, but it hasn't
changed, so oh well)
     'a' : echo 'Called Dynamically<br />';
     'b' : echo 'Called Statically<br />';
   }
}

or, is_a() to include descendants (PHP 4.2.0+ I think)

function Go()
{
    if (!isset($this))
    {
        echo 'Called Statically<br />';
    }
   if (is_a($this,'a'))
  {
      echo 'Called Dynamically<br />';
   } else
   {
      echo 'Called Statically<br />';
   }
}

Incidentally, this ability to call other methods and operate on your own
variables is in my opinion the greatest feature of PHP along with
aggregation.  It allows dynamic (runtime!) multiple inheritance, which is
essential for the new version of a complex data-access project I'm working
on constantly (http://calendar.chiaraquartet.net)

Take care,
Greg
--
phpDocumentor
http://www.phpdoc.org

"Sean Malloy" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Consider the following code...
>
> class A
> {
>   function Go()
>   {
>     switch (isset($this))
>     {
>       case true: echo 'Called Dynamically<br />'; break;
>       case false: echo 'Called Statically<br />'; break;
>     }
>   }
> }
>
> class B
> {
>   function Go()
>   {
>     A::Go();
>   }
> }
>
> A::Go();
> $a = new A();
> $a->Go();
>
> B::Go();
> $b = new B();
> $b->Go();
>
>
> My understanding is that the output should be:
>
> Called Statically
> Called Dynamically
> Called Statically
> Called Statically
>
> Yet the output is actually:
>
> Called Statically
> Called Dynamically
> Called Statically
> Called Dynamically
>
>
> Now my question is, is this what was intended?
>
> It seems that if you call a static class from within a dynamic instance of
a
> class, the static class then decides $this should reference the class from
> which the static class was called
>
> Anyone else come across this?
>
> It could be useful, but right now, its bloody annoying! I need a class to
be
> called from within another clas, and it needs to know wether it has had an
> instance created, or wether it is being statically called, and now I'll
have
> to write some kludge code instead...
>
>
>
>
> ///////////////////////////////////////////////////////////////////
> // Sean Malloy
> // Developer
> // element
> // t: +61 3 9510 7777
> // f: +61 3 9510 7755
> // m: 0413 383 683
> ///////////////////////////////////////////////////////////////////
>
> DISCLAIMER:
> © copyright protected element digital pty ltd 2002.
> the information contained herein is the intellectual property
> of element digital pty ltd and may contain confidential material.
> you must not disclose, reproduce, copy, or use this information
> in any way unless authorised by element digital pty ltd in writing
> or except as permitted by any applicable laws including the
> copyright act 1968 (cth).
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to