I am by no means an expert with OOP, so if this is a blatantly retarded
question please excuse my ignorance.
Given this code:
<?php
class A
{
function foo()
{
echo METHOD;
}
}
class B
{
function bar()
{
// blah
}
}
$instance = B::foo();
?>
output is "A::foo".
Is this correct? I would expect (want maybe :) ) to see output as
"B::foo". If you need more information please let me know.
TIA
-dan
"Daniel J Cain Jr ." dan@cain.sh wrote in message
news:FBCF2135-AB83-11D8-A867-000A95BE1F8C@cain.sh...
$instance = B::foo();
?>output is "A::foo".
Is this correct? I would expect (want maybe :) ) to see output as
"B::foo".
this is as designed. METHOD is a compile-time evaluated token, not a
runtime evaluated function.
l0t3k
Daniel J Cain Jr. wrote:
I am by no means an expert with OOP, so if this is a blatantly retarded
question please excuse my ignorance.Given this code:
<?php
class A
{
function foo()
{
echo METHOD;
}
}class B
{
function bar()
{
// blah
}
}$instance = B::foo();
?>output is "A::foo".
Is this correct? I would expect (want maybe :) ) to see output as
"B::foo". If you need more information please let me know.TIA
-dan
Hi,
this "constant" like the others is resolved on compile time. Another example is CLASS:
php -r 'class a{ function c() {var_dump(CLASS);}}class b extends a{};$a=new b();$a->c();'
dumps :
string(1) "a"
METHOD is resolved as CLASS.'::'.FUNCTION
IMO the current behaviour is the normal since works like the C preprocessor doing
substitutions before compile (PHP does it while compiling). You can get what you wanted
with this :
echo get_class($this).'::'.FUNCTION (when there is an instance of the class) but AFAIK
in your case with static calls there is no solution.
Cheers,
Andrey
Hi,
Andrey Hristov wrote:
echo get_class($this).'::'.FUNCTION (when there is an instance of the
class) but AFAIK in your case with static calls there is no solution.
Is there some way to add a function (or some other magic thing) that works
with static calls and call it a bug fix, so it can be in 5.0? If not: Is
there a way to implement it and add it as a new feature with PHP5.1?
Lately I needed such a thing while building some kind of framework. I solved
it by adding a function to my extended class which passes it's CLASS as
an additional paramter to my base function - I don't really like it that
way ;-)
johannes
debug_backtrace();
Not the prettiest solution, but a reliable one nonetheless.
-Sara
"Johannes Schlueter" schlueter@phpbar.de wrote in message
news:20040522084054.30545.qmail@pb1.pair.com...
Hi,
Andrey Hristov wrote:
echo get_class($this).'::'.FUNCTION (when there is an instance of
the
class) but AFAIK in your case with static calls there is no solution.Is there some way to add a function (or some other magic thing) that works
with static calls and call it a bug fix, so it can be in 5.0? If not: Is
there a way to implement it and add it as a new feature with PHP5.1?
Lately I needed such a thing while building some kind of framework. I
solved
it by adding a function to my extended class which passes it's CLASS
as
an additional paramter to my base function - I don't really like it that
way ;-)johannes
Hi,
Not really, too.
<?php
class a {
static public function foo() {
print_r(debug_backtrace());
}
}
class b extends a {}
a::foo();
b::foo();
?>
Array
(
[0] => Array
(
[file] => -
[line] => 8
[function] => foo
[class] => a
[type] => ::
[args] => Array
(
)
)
)
Array
(
[0] => Array
(
[file] => -
[line] => 8
[function] => foo
[class] => a
[type] => ::
[args] => Array
(
)
)
)
in both cases it tells [class] => a, so one still needs to extend the
foo()-Method:
<?php
class a {
static public function foo() {
print_r(debug_backtrace());
}
}
class b extends a {
static public function foo() {
parent::foo();
}
}
a::foo();
b::foo();
?>
Array
(
[0] => Array
(
[file] => -
[line] => 15
[function] => foo
[class] => a
[type] => ::
[args] => Array
(
)
)
)
Array
(
[0] => Array
(
[file] => -
[line] => 11
[function] => foo
[class] => a
[type] => ::
[args] => Array
(
)
)
[1] => Array
(
[file] => -
[line] => 16
[function] => foo
[class] => b
[type] => ::
[args] => Array
(
)
)
)
Asyou can see at $bt = debug_backtrace()
; $bt[1]['class'] the name is
available - but only if the function wasextended like in the example....
johannes
Sara Golemon wrote:
debug_backtrace()
;Not the prettiest solution, but a reliable one nonetheless.
-Sara
"Johannes Schlueter" schlueter@phpbar.de wrote in message
news:20040522084054.30545.qmail@pb1.pair.com...Hi,
Andrey Hristov wrote:
echo get_class($this).'::'.FUNCTION (when there is an instance of
the
class) but AFAIK in your case with static calls there is no solution.Is there some way to add a function (or some other magic thing) that
works with static calls and call it a bug fix, so it can be in 5.0? If
not: Is there a way to implement it and add it as a new feature with
PHP5.1? Lately I needed such a thing while building some kind of
framework. I
solved
it by adding a function to my extended class which passes it's CLASS
as
an additional paramter to my base function - I don't really like it that
way ;-)johannes