0

I am trying to make a database connection in PHP using two different classes. There is an error in my object creation:

Error: Parse error: syntax error, unexpected '$obj' (T_VARIABLE), expecting function (T_FUNCTION)

The error occurs on this line: $obj = new UserController();

This is my Connection class:

class Connection
{
    public $servername = "localhost";
    public $username = "root";
    public $password = "";
    public $dbname = "web-portal";


    function database (){    
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            return $conn;
        } else {
            return $conn;
        }
    }
}

This is my UserController class:

class UserController extends Connection  {      
    $obj = new UserController();   
    //$obj->database(); 

    function getUser ($userId) {

        // Sql query to select record from user table
        $sql = "SELECT *  FROM User where user_id = ".$userId;

        //Excute  sql query
        $result = $conn->query($sql);

        //If record exists
        if ($result->num_rows > 0) {
            // output data of each row
            $row = $result->fetch_assoc();
            $conn->close();
            return $row;
        } else { 
            //If no record exists return row 0
            $row = 0;
            return  $row;
        }
    }
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Andrew Hall
  • 59
  • 1
  • 9
  • Error #1 `$obj = new UserController(); ` you can't define a property with a value that depends on run-time information; you should be setting this value in a constructor.... but why are you trying to set it to an instance of itself? – Mark Baker Mar 09 '16 at 20:22
  • I want make database connection – Andrew Hall Mar 09 '16 at 20:24
  • Error #2 `$result = $conn->query($sql);` `$conn` isn't in scope for the `getUser()` method..... use `$this->database()->query($sql);` instead.... although making a new connection every time you want to execute a `getUser()` request is inefficient – Mark Baker Mar 09 '16 at 20:25
  • How I can acces get user from my dashboard php file – Andrew Hall Mar 09 '16 at 20:30
  • OOP 101..... you instantiate the class (`$user = new UserController();`) and then you call your `getUser()` method for that instance (`$x = $user->getUser($userId);`) – Mark Baker Mar 09 '16 at 20:36

1 Answers1

0

Some general guidance:

  • Object orientation focusses on things (such as a database connection) and inheritance, which allows a specialisation of things (e.g. a MySQL database connection is type of database connection). However a UserController is not a Connection, so this is not a good inheritance - a UserController should inherit from Controller or something similar.
  • If you wish to import database functionality, you could use a trait. That allows you to import methods that do not belong to a parent or ancestor class. However, traits are not completely necessary for OO design, and if you are a beginner you may wish to leave them alone for now.
  • In your controller, you want to obtain a connection, so rather than $result = $conn->query($sql) you probably want $this->database()->query($sql). $conn is not available to you anyway.
  • Your query has a SQL injection vulnerability, so do not put code of this kind live.
  • You will need to do $obj = new UserController() but this would belong in another file, something like this:

    require_once 'Connection.php';
    require_once 'UserController.php';
    
    $controller = new UserController();
    $row = $controller->getuser(1);
    print_r($row);
    

You may find that a good approach is to work through some good tutorials on the web - it may be faster in the long term than struggling on a project that is not guided.

halfer
  • 19,824
  • 17
  • 99
  • 186