# $this availability inside static-functions

- Thread: https://externals.io/message/17362
- Original archive: https://news-web.php.net/php.internals/17362
- Started by Sebastian Mendel on 2005-07-21T16:11:51+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #17362 — Sebastian Mendel — 2005-07-21T16:11:51+00:00

URL: https://externals.io/message/17362#17362

Hi,

at first, im really really sorry to bother the internals-list with this
problem (especially Derick for coming back from playing somewhere else
... (<Pine.LNX.4.62.0506291622130.14983@localhost>) ), but i didnt find
an answer on php.general or german php mailinglist

if anybody just can point me to any discussions, websites, bugs or
anything else related to the problem below. or just what is the point
behind this behaviour?


*PHP 4*

how can i check if a method is called statically when called from inside
another object? (without debug_bactrace())

class foo
{
    function bar()
    {
        if ( isset( $this ) )
        {
            return 'not static';
        }
        return 'static';
    }

    function bar2() { foo::bar(); }
}


// returns 'static'
echo foo::bar();

// returns 'not static' but should be 'static'
$foo = new foo;
echo $foo->bar2();


*PHP 5*

and btw. does anyone know why it is not good to let one function handle
both methods of calling (static and not static) as in PHP 5 you will get
notices if called wrong - but PHP doesnt support multiple
function-definitions like:

class foo
{
    static function bar()
    {
        return ANY_DEFAULT_VALUE;
    }

    function bar()
    {
        return $this->value;
    }
}

or


class foo
{
    static function bar( $param )
    {
        return $param;
    }

    function bar()
    {
        return $this->param;
    }
}


-- 
Sebastian Mendel

www.sebastianmendel.de
www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet

---

## #17363 — Sean Coates — 2005-07-21T16:16:25+00:00

URL: https://externals.io/message/17362#17363 · In reply to: #17362

> how can i check if a method is called statically when called from inside
> another object? (without debug_bactrace())

<plug 
type="own">http://blog.phpdoc.info/archives/4-Schizophrenic-Methods.html</plug>

Short answer:
$isStatic = !(isset($this) && get_class($this) == __CLASS__);

HTH

S

---

## #17369 — Sebastian Mendel — 2005-07-21T20:09:45+00:00

URL: https://externals.io/message/17362#17369 · In reply to: #17363

Sean Coates schrieb:
> > […]
> 
> <plug
> type="own">http://blog.phpdoc.info/archives/4-Schizophrenic-Methods.html</plug>
> 
> Short answer:
> $isStatic = !(isset($this) && get_class($this) == __CLASS__);

i know this hack, but it does not work, if the statically called method
is from the same class as the calling object

class foo
{
    function bar()
    {
        if ( isset( $this ) && get_class( $this ) == __CLASS__ )
        {
            return 'not static';
        }
        return 'static';
    }

    function bar2() { return foo::bar(); }
}


// returns 'static'
echo foo::bar();

// returns 'not static' but should be 'static'
$foo = new foo;
echo $foo->bar2();


-- 
Sebastian Mendel

www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
www.sf.net/projects/phpdatetime        www.sf.net/projects/phptimesheet

---

## #17370 — Sean Coates — 2005-07-21T20:17:56+00:00

URL: https://externals.io/message/17362#17370 · In reply to: #17369

> i know this hack, but it does not work, if the statically called method
> is from the same class as the calling object

Good point. Sorry for not noticing.
.. I have no idea WHY someone would want to do this.. that said, I don't 
know of a way to do it, either.

S

---

## #17373 — Sebastian Mendel — 2005-07-22T06:59:20+00:00

URL: https://externals.io/message/17362#17373 · In reply to: #17370

Sean Coates wrote:
> > […]
> 
> Good point. Sorry for not noticing.
> ... I have no idea WHY someone would want to do this.. that said, I
> don't know of a way to do it, either.

a function who returns a name of an object, say its name is

$object->getName();

this needs that the object exists and loaded

how about if i need a name for object not loaded?

Class::getName( $object_id );


there is really no need to load/create the whole object if i only need
the name, so doing

$object = new Class( $object_id );
$object->getName();

would be a waste of system-resources

f.e. the same applies to any other properties of an object stored in a DB.

but why should i choose for the same function two different method-names?

f.e.

Class::getName()
static Class::staticGetName()

??

this doesnt makes sence to me.


pls, give me a hint if i had overseen something.


-- 
Sebastian Mendel

www.sebastianmendel.de
www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet

---

## #17376 — ghostwwinside@gmx.net — 2005-07-22T09:23:13+00:00

URL: https://externals.io/message/17362#17376 · In reply to: #17373

Sebastian Mendel wrote:
>
> how about if i need a name for object not loaded?
> 
> Class::getName( $object_id );
> 

Weird, i thought you must know the classname for a static
call :)
