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
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
Sean Coates schrieb:
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);
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
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
Sean Coates wrote:
i know this hack, but it does not work, if the statically called method
is from the same class as the calling objectGood 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
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 :)