php OOP login/register videos

+1 jamie y · November 16, 2014
I have been following the project for the oop login / register video series here on this site. I ran into a problem that I can't solve. There is a function that the author tells us to make at 4:06 minutes in. I get a syntax error every time I try to create the function the way the author says to make it which is like so



    public function first()
    {
        return $this->results()[0];
    }


The error I get is


Parse error: syntax error, unexpected '['



Anyone got any idea?

Just in case it's needed, here is all the code of the class thus far.




class DB {
    
    private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_results,
            $_count = 0;
            
    private function __construct()
    {
        try {
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), config::get('mysql/password'));
        } catch(PDOException $e){
            die($e->getMessage());
        }
    }
    
    public static function getInstance()
    {
        if(!isset(self::$_instance)){
            self::$_instance = new DB();
        }
        return self::$_instance;
    }
    
    public function query($sql, $params = array())
    {
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param) {
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
            
            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
                }
            }
            return $this;
        }
        
    public function action($action, $table, $where = array())
    {
        if(count($where) === 3) {
            $operators = array('=', '>', '<', '>=', '<=');
            
            $field    = $where[0];
            $operator = $where[1];
            $value    = $where[2];
            
            if(in_array($operator, $operators)){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";    
                if(!$this->query($sql, array($value))->error()){
                    return $this;
                }
            }
        }
        return false;
    }
    
    public function get($table, $where)
    {
        return $this->action('SELECT *', $table, $where);
    }
    
    public function delete($table, $where)
    {
        return $this->action('DELETE *', $table, $where);
    }
    
    public function insert($table, $fields = array())
    {
        if(count($fields)){
            $keys = array_keys($fields);
            $values = null;
            $x = 1;
            
            foreach($fields as $field){
                $values .= "?";
                if($x < count($fields)){
                    $values .= ', ';    
                }
                $x++;
            }
            die($values);
            $sql = "INSERT INTO users (`".implode('`, `', $keys)."`) VALUES ({$VALUES})";
            
            if(!$this->query($sql, $fields)->error()){
                return true;
            }
        }
        return $this->query()->error();
    }
    
    public function results()
    {
        return $this->_results;    
    }
    
    public function first()
    {
        //original code was return $this->results()[0]; gave syntax error, unexpected [ on line 87
        return $this->results()[0];
    }
        
    public function error()
    {
        return $this->_error;
    }
    
    public function count()
    {
        return $this->_count;    
    }
}


Post a Reply

Replies

Oldest  Newest  Rating
+1 Ron Butcher · November 16, 2014
What version of PHP are you running?  I think this is a PHP 5.4 thing.

I haven't tried that shortcut before, but you can get the same results with no error this way:


public function first()
{
return $this->_results[0];
}



or:


public function first()
{
$res = $this->results();
return $res[0];
}



You should probably also initialize $_results as an array at the top of the page.  That way it is understood to be an array throughout the page.


private $_pdo, 
            $_query, 
            $_error = false, 
            $_results = array(), // <-------- Initalize
            $_count = 0;
  • 1

PHP

107,335 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators