Could this be considered a well-written class (am I using OOP correctly)?

Posted by Ben Dauphinee on Stack Overflow See other posts from Stack Overflow or by Ben Dauphinee
Published on 2010-05-29T22:24:14Z Indexed on 2010/05/29 22:52 UTC
Read the original article Hit count: 163

I have been learning OOP principals on my own for a while, and taken a few cracks at writing classes. What I really need to know now is if I am actually using what I have learned correctly, or if I could improve as far as OOP is concerned.

I have chopped a massive portion of code out of a class that I have been working on for a while now, and pasted it here.

To all you skilled and knowledgeable programmers here I ask: Am I doing it wrong?

class acl extends genericAPI{
    // -- Copied from genericAPI class
    protected final function sanityCheck($what, $check, $vars){
        switch($check){
            case 'set':
                if(isset($vars[$what])){return(1);}else{return(0);}
                break;
        }
    }
    // ---------------------------------

    protected $db = null;
    protected $dataQuery = null;

    public function __construct(Zend_Db_Adapter_Abstract $db, $config = array()){
        $this->db = $db;
        if(!empty($config)){$this->config = $config;}
    }

    protected function _buildQuery($selectType = null, $vars = array()){
        // Removed switches for simplicity sake
        $this->dataQuery = $this->db->select(
            )->from(
                $this->config['table_users'],
                array('tf' => '(CASE WHEN count(*) > 0 THEN 1 ELSE 0 END)')
            )->where(
                $this->config['uidcol'] . ' = ?',
                $vars['uid']
            );
    }

    protected function _sanityRun_acl($sanitycheck, &$vars){
        switch($sanitycheck){
            case 'uid_set':
                if(!$this->sanityCheck('uid', 'set', $vars)){
                    throw new Exception(ERR_ACL_NOUID);
                }
                $vars['uid'] = settype($vars['uid'], 'integer');
                break;
        }
    }

    private function user($action = null, $vars = array()){
        switch($action){
            case 'exists':
                $this->_sanityRun_acl('uid_set', $vars);
                $this->_buildQuery('user_exists_idcheck', $vars);
                return($this->db->fetchOne($this->dataQuery->__toString()));
                break;
        }
    }

    public function user_exists($uid){
        return($this->user('exists', array('uid' => $uid)));
    }

}

$return = $acl_test->user_exists(1);

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about oop