开发者

objected returned as boolean inside if clause

I don't know enough about when and ho开发者_运维知识库w various variables are returned. Considering I have an conditional statement with an object to validate inside of this. Am I right that this is returned as a boolean value.

if($id = $oE->validate($_POST, $_FILES)){
...
}

What I really want is for this to return an array of errors if there are any errors, otherwise it will return the $id of the updated content.

With this above, it seems to return a boolean true if any value at all is returned by the validate() object??


PHP interprets any non-zero value as true. What you need is to pass a reference which holds any error codes, and get the function to either return false on failure or the id on success. ie:

class Validator()
    {
    function validate($post,$files,$errorRef)
        {
        //Your code here
        if ($success)
            {
            return $id;
            }
        else
            {
            $errorRef = $errorCode;
            return false;
            }
        }
    }
//
$oE = new Validator;
$error = NULL;
$id = $oE->validate($_POST,$_FILES,&$error);
if ($id !== false) //If validator did not return false
    {
    //Stuff happens
    }
else
    {
    switch ($error)
        {
        //Error Handling Stuff
        }
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜