开发者

php not throwing an Exception in function overriding

I have a class named DataBoundObject which is quite big. And works good. Its an ORM class i have developed. One of its functions is to automatically do the setXXX() and getXXX() functions so 开发者_JS百科that no coding is required. The following function does that

public function __call($strFunction, $arArguments) {
        $strMethodType = substr ( $strFunction, 0, 3 );
        $strMethodMember = substr ( $strFunction, 3 );

        if(!is_callable(array($this, $strFunction)))
            throw new Exception("Function cannot be called!"); 

        switch ($strMethodType) {
            case "set" :
                return ($this->SetAccessor ( $strMethodMember, $arArguments [0] ));
                break;
            case "get" :
                return ($this->GetAccessor ( $strMethodMember ));
                break;
            default :
                throw new Exception ( "Non existent method call dude!" );
        }
        return false;
    }

Now in a class which derives this i override one function like this:

<?php

require_once ('DataBoundObject.php');
/** 
 * ORM extension of BUBBLES_HOTEL_REVIEWS Tabel
 * @author footy
 * @copyright Ajitah
 * 
 */
class reviews extends DataBoundObject {
    protected $ReviewID;
    //Other codes

    private function setReviewID($ReviewID) {
        throw new Exception ( "Cannot set the review ID Explicitly" );
    }
    //Other codes
    //Other codes
    //Other codes
    //Other codes
    //Other codes    
}
$x = new reviews();
$x->setReviewID(5);

?>

Thus finally i create a new object and try to call setReviewID() function which is private. Why is it not generating any Exception? Besides is_callable() is returning true!

EDIT

Mainly i need help to correct this problem so that it throws an Exception


You can't override private methods using __call magic in PHP. I allow myself to quote from php.net website http://php.net/language.oop5.visibility#92995, where your question is perfectly answered in a comment:

  1. In the overriding, the method names and arguments (arg’s) must be same.

  2. final methods can’t be overridden.

  3. private methods never participate in the in the overriding because these methods are not visible in the child classes.

  4. While overriding decreasing access specifier is not allowed

If you desperately need this feature - your (unconventional) options will be:

  1. Use public scope for the method, documenting the function of its restrictions for other developers in PHPDoc string.
  2. You can use PECL extensions like runkit sandbox http://php.net/book.runkit
  3. Go for code generation or preprocessor.
  4. Choose a different language.

EDIT

Note that, protected child method is also hidden form the parent, but there is always an option of overriding __call() in a child. Generally, making too much "magical" overrides may be a bad taste for such a serious undertaken as ORM design.

PS

I believe eventually developing a DSL could be an ultimate goal for your project. Before doing so, continuing your project is a nice way to gather respective experience. GL!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜