Richard Sabol
Richard Sabol

Reputation: 21

Catchable error - Argument must be an instance of PDO

Hey guys i am getting this ERROR:

Catchable fatal error: Argument 1 passed to Users::__construct() must be an instance of PDO, none given, called in C:\xampp\htdocs\xampp\ooplogin\register.php on line 46 and defined in C:\xampp\htdocs\xampp\ooplogin\class\Users.php on line 9

to say true code is working with no problem but u know if i want to make clean code it shouldbt be any error there.

My code:

<?php 
class Users {
    /* Deklarovanie premennych */
    public $name = null;
    public $email = null;
    public $phone = null;
    public $message = null;

    public function __construct(PDO $db_con)
    {
        $this->db_con = $db_con;
    }

    public function storeFormValues( $data )
    {
        if( isset( $data['name'] ) ) $this->name = stripslashes( strip_tags( $data['name'] ) );
        if( isset( $data['email'] ) ) $this->email = stripslashes( strip_tags( $data['email'] ) );
        if( isset( $data['phone'] ) ) $this->phone = stripslashes( strip_tags( $data['phone'] ) );
        if( isset( $data['message'] ) ) $this->message = stripslashes( strip_tags( $data['message'] ) );
        return $this;
    }

    public function message()
    {
        $correct = false;
        try {
            $sql = "INSERT INTO user(name, email, phone, message) VALUES (:name, :email, :phone, :message)";

            $stmt = $this->db_con->prepare( $sql );
            $stmt->bindValue( "name", $this->name, PDO::PARAM_STR );
            $stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
            $stmt->bindValue( "phone", $this->phone, PDO::PARAM_STR );
            $stmt->bindValue( "message", $this->message, PDO::PARAM_STR );
            $stmt->execute();
            return 'Sprava bola uspesne odoslana!';
        }catch( PDOException $e ) {
            return $e->getMessage();
        }
    }

    public function displayAll()
    {
        try{
            $sql = "SELECT * FROM users LIMIT 10";
            $stmt = $this->db_con->prepare($sql);
            $stmt->execute();
            return $this->$stmt;       
        }catch( PDOException $e ) {
            return $e->getMessage();
        }  
    }    
}

try {
    $db_con = new PDO( DB_HOST, DB_USER, DB_PASS );
    $db_con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $db_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch( PDOException $e ) {
    return $e->getMessage();
}


$Users = new Users($db_con);
$Users->storeFormValues( $_POST );

echo $Users->message();
?>

And my Index.php looks like:

<?php if( !(isset( $_POST['send'] ) ) ) { ?>

HTML FORM HERE 

<?php 
} else {
  $Users = new Users;
    $Users->storeFormValues( $_POST );
    if( $Users->message() ) {
        echo "Sprava bola úspešne odoslana";    
    } else {
        echo "Sprava nebola odoslana";    
    }
}

?>

Is here a chance somebody can help me to fix it or explain why its not working? Thanks!

Upvotes: 0

Views: 3886

Answers (1)

previous_developer
previous_developer

Reputation: 10988

You are not passing a PDO instance like error message says.

$Users = new Users;

Edit: First of all, do not copy/paste code and expect it to work. I will tell you what to do, but not give you any code.

When you create a class with a parameter (with no default value) in its constructor, you have to pass parameter to make it work. This is why you get this error. The line I show you is where error thrown. Users class require a PDO object, but you passed nothing. Though you did it in your class file:

$Users = new Users($db_con);

Of cource you need you also need to initialize $db_con variable as you did in your class file.

This will fix your problem. I also have a couple of suggestions.

  1. Put your every class in a seperate file and do not execute any code out of your class in these files.
  2. Put your connection ($db_conn initialization) in a seperate file so you can call it anywhere you need it again.
  3. See include and require functions to learn how you can get code from other files to apply 1 and 2.

These are very basic suggestions. There are more complicated but better ways to do same things, like autoloading and IoC containers. But when starting to code, you can leave these concepts to learn them later for sake of your sanity. :)

Upvotes: 1

Related Questions