views:

235

answers:

5

hi, is __call() function availabe in CakePHP? I used this fucntion in Zend Framework.

class UsersController extends AppController {
function home(){
/*some action*/
}

function __call($m, $p){
print_r($m);
print_r($p);
}
}

getting erro like this

Missing Method in UsersController

<?php

class UsersController extends AppController {

 var $name = 'Users';


 function somemethodsnotincontoller() {

 }

}
?>

for the URL site.com/users/somemethodsnotincontoller

+2  A: 

Used it for what?

The __call() method is a construct in PHP that you can use from within classes that allow you to "catch" calls to methods that don't exist in the class explicitly.

From PHP.net:

__call() is triggered when invoking inaccessible methods in an object context.

So the answer is yes, as long as you are using PHP 5 or up.

snicker
+1  A: 

__call() is a language construct so it is available in all versions of php that support it.

Byron Whitlock
A: 

__call is one of PHP 5's magic methods (see "Method overloading" for more details).

If you are using PHP 5 (and you are, if you are usinf Zend Framework), you can have a __call method in your classes, not depending on the framework you are working with.

Pascal MARTIN
A: 

__call() is a magic-method of PHP, not any particular framework. It's impossible to answer this question without any context since __call() is defined in a particular object, not globally. Since CakePHP touts the fact that it is php4 compatible and __call() was introduced in php5 I would say no.

I looked at the production branch for Models and there is a call__() method, which looks like it tries to emulate PHP5's __call().

https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/model/model.php?rev=4211#L437


Edit (Responding to comment):

Looking at Cake's base controller, there does not appear to be a 'catch-all' method available in controllers that mimics Zend's implementation of __call(). Your alternative to accomplish this would be to setup a route similar to cake's page route to catch all actions directed at a controller and send them to a single method.

Cake Trac for base controller: https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/controller/controller.php?rev=4211

Cake documentation on routing: http://book.cakephp.org/view/46/Routes-Configuration

One of the examples in that documentation I referenced looks like something you can play with to accomplish what I mentioned above:

Router::connect(
    '/cooks/:action/*', array('controller' => 'users', 'action' => 'index')
);

Regardless of the given-action, always use the index action.

Mike B
CakePHP does use __call, though.
Chris Kloberdanz
please check my Qustion updates
coderex
+3  A: 

As many have pointed out here, __call() is a native PHP5 language "magic" method for catching calls to class methods that don't exist.

HOWEVER, Cake's core (I think it's the dispatcher) checks to see if the method exists first before calling it, and if it doesn't it renders the missing method error.

A solution might be for you to create your own AppError class, and handle the "catch all" method in there.

There is a limited amount of information in the cook book under Error handling

neilcrookes