1

Okay, i have little MVC what works like

somesite/classname/classfunction/function

class test(){
 public function test2(){
 // action will be 'function' in adress
 $action = $this->action ? $this->action : array($this, 'test3');
 function test3(){
 print 1;
 }
 $action();
 }
}

So, if we run somesite/test/test2/test3 it will print '1', but if we run somesite/test/test2/phpinfo it will show phpinfo.
Question: how to check existing of function in class function ?

UPD
Don't forget about phpinfo, function_exists will show it. method_exists search in class functions, but not in functions of class function
UPD Solution

class test{
 public function test2(){
 // site/test/test2/test3
 $tmpAction = $this->parenter->actions[1]; // test3 
 $test3 = function(){
 print 1;
 };
 if(isset($$tmpAction)){
 $$tmpAction();
 }else{
 $this->someDafaultFunc();
 }
 }
}
asked Jan 16, 2013 at 8:08
6
  • Functions inside the class called METHODS. So, inside the class, simply - if (method_exists($this, 'methodName')) Commented Jan 16, 2013 at 8:14
  • That test3 inside test2 cannot be invoked using array($this, 'test3'). You should avoid functions within functions. Hoping it's just a bad copy-n-paste example. Commented Jan 16, 2013 at 8:15
  • @deceze avoid functions inside functions in general, or in php ? Commented Jan 16, 2013 at 8:16
  • @metal PHP obviously. In other languages functions within functions are a core design pattern. Note I also don't mean anonymous functions. I mean bare, global, conditional function declarations inside another function. Commented Jan 16, 2013 at 8:17
  • php.net/manual/en/function.get-defined-functions.php gives me defined functions. Nicely grouped to system and user. That's a way to weed phpinfo out. Still, I'd use method_exists and make test3 a class method to make it clean OOP. Commented Jan 16, 2013 at 8:21

3 Answers 3

2

http://php.net/function-exists

http://php.net/method-exists

if ( function_exists('function_name') ) {
 // do something
}
if ( method_exists($obj, 'method_name') ) { /* */ }

You should also check out the magic method __call()

KingCrunch
133k22 gold badges158 silver badges174 bronze badges
answered Jan 16, 2013 at 8:10
Sign up to request clarification or add additional context in comments.

Comments

2

To check whether a certain method in a class exists, use: http://php.net/method-exists

 $c = new SomeClass();
 if (method_exists($c, "someMethod")) {
 $c->someMethod();
 }

You are also allowed to use the class name:

 if (method_exists("SomeClass", "someMethod")) {
 $c = new SomeClass();
 $c->someMethod();
 }

To "fix" your problem, make test3() a class method:

class test(){
 private function test3() {
 print 1;
 }
 public function test2(){
 // action will be 'function' in adress
 $action = $this->action ? $this->action : array($this, 'test3');
 if (method_exists($this, $action)) {
 $this->$action();
 } else {
 echo "Hey, you cannot call that!";
 }
 }
}
KingCrunch
133k22 gold badges158 silver badges174 bronze badges
answered Jan 16, 2013 at 8:11

5 Comments

i dont need to use test3 as method of class, only as function of class method.
bcz if it will be a class method it will be as function and can be called from MVC structure like /test/test3
@Abyss Ah. I get it. Perhaps you should explain that in the question.
@Abyss: also if you declare it private ?
I was getting there as well, but my PHP install didn't have anonymous functions, so my testbed didn't work ;-). You could answer your own question if you want.
2
class test{
 public function test2(){
 // site/test/test2/test3
 $tmpAction = $this->parenter->actions[1]; // test3 
 $test3 = function(){
 print 1;
 };
 if(isset($$tmpAction)){
 $$tmpAction();
 }else{
 $this->someDafaultFunc();
 }
 }
}
answered Jan 16, 2013 at 8:46

1 Comment

Note that anonymous functions are supported from PHP 5.3.0.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.