[thelist] PHP conditionally create a class method?

sbeam sbeam at onsetcorps.net
Wed Aug 27 19:57:36 CDT 2008


On Wednesday 27 August 2008 19:24, Stephen Rider wrote:
>> bar() is only known within the scope of foo()
>
> That statement is flatly in opposition to the O'Reilly PHP book.  It  
> states (page 66):
>
> *** quote ***
> You can nest function declarations, but with limited effect.  Nested  
> declarations do not limit the visibility of the inner defined  
> function, which may be called from anywhere in your program.
> *** end quote ***

well the book is sort of correct. But inside a class things are different.

class test { function foo() {  function bar() { echo "hi\n"; } bar(); } }
test::foo();
test::bar();

===
hi
PHP Fatal error:  Call to undefined method test::bar() in /tmp- on line 3
===

why? good question... 


> Just FYI, as your first point precludes using that method anyway.

Of course :) Since I have never seen it done before in PHP I had to try it out 
and comment.


> Okay, _that_ was my main confusion.  I thought that would result in  
> more "You've already defined that function" errors.  I'll give that a  
> try.

quite so. In fact that is the basic concept of inheritance at work. Wouldn't 
be an OO paradigm without it.
http://en.wikipedia.org/wiki/Inheritance_(computer_science)

Sam



More information about the thelist mailing list