tags:

views:

96

answers:

1

Hi all,

In a controller, I got two functions that one is made to be private:

function toavail(){ 
             $this->autoRender=false;                
             $result2=$this->__avail();                  
              if($result2==0){return "OK";}
              else{return 0;}                                                  
        }

function __avail(){  
              $result1=$this->Site1->findByusername('1');  
               if($result1){
                return 1;
                            }
               else{
                 return 0;
                   }
        } 

I am not sure if it is a proper way to access the private function in this case.

+2  A: 

You're accessing it correctly (assuming that both methods are in the same controller class), but in case you're not aware, your __avail() method isn't really private. The double underscore (__) prefix is something of a convention, but it's only a convention. Your "private" method is really public in actuality. To make it private you need to specify it as such in the signature:

private function __avail() { ... }
Rob Wilkerson
Double underscore prefix is not just a convention, CakePHP will treat this method as a private method which is not accessible as a controller's action even using PHP4.
iwat
Good info. I wasn't aware of that wiring and can't seem to find any reference to it anywhere. I can only find it referenced as a convention. Can you cite your source with a URI?
Rob Wilkerson
That's a bad practice to use __ as a prefix for functions. According to php documentation `PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.` So, avoid using __ in your controller functions.
bancer
@bancer this night be true but the core cake files are full of '__' functions
Angel S. Moreno
@Angel: that was implemented for PHP4 very long time ago. No one would repeat that now.
bancer
Interesting, I have not check the core of 1.3 but as of CakeFest last weekend CakePHP 2.0 will not be PHP4 compatible.
Angel S. Moreno