PHP Define a new PDO object inside class (outside function)

Dan Lindqvist

I am making a class with a couple function that requires some SQL stuff, and I do not want define a new PDO object for every function, but have a one that works on all functions.

This is what I got so far:

<?php
class UserSystem
{
    public $mysqlDetails = 
    [
        "host"        => "127.0.0.1",
        "user"        => "",
        "pass"        => "",
        "database"    => "",
        "table"       => ""
    ];

    public $con = new PDO
    (
        "mysql:host=" . $this->mysqlDetails['host'] .
        ";dbname=" .    $this->mysqlDetails['database'] . ";",
                        $this->mysqlDetails['user'],
                        $this->mysqlDetails['pass']
    );

How could I get this to work? Currently I get this error: Parse error: syntax error, unexpected 'new' (T_NEW). Is there a better way to do this, so I can use the $con variable in functions without having to create a new one. I do not want to inject it into functions as I've seen suggested. IE this:

<?php
function someFunc ($pdo)
{
    $pdo->query("SELECT * FROM `someTable`");
}
deceze

You cannot initialize class properties with anything but compile time resolvable constant values. You'll have to create the PDO object in the constructor:

public function __construct() {
    $this->con = new PDO(...);
}

Better yet, you should make your class accept a PDO object and dependency inject it, since I'd be guessing you need a database connection in more than just this one class, and you don't want to connect to the same database again and again in different classes:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function define a new object class

From Dev

PHP Call outside class function inside anonymous function

From Dev

Why are PHP tags not allowed inside a class but outside a function?

From Dev

PHP PDO injection inside a class

From Dev

How to define a member function outside class in python?

From Dev

Define a Function inside an Object with a variable as part of the function

From Dev

PHP function - works outside function, but not inside

From Dev

How to use an external variable inside a class/function/object in php

From Dev

Define member variables of class inside function

From Dev

function declaration inside object/class

From Dev

C++ - Define member function outside template-class but in header

From Dev

Can I define a private template function outside of a class?

From Dev

PHP pdo class getadresses warning and Call to a member function prepare() on a non-object

From Dev

Friend function inside class and outside class, what difference does it make?

From Dev

Define new function, array, struct etc inside of parameter of function call

From Dev

Define new function, array, struct etc inside of parameter of function call

From Dev

php public function is not working outside of the class

From Dev

Declare object outside function, pass it as reference and then create it inside function

From Java

Why is a collection initializer without `new` allowed inside an object initializer but not outside?

From Dev

How to correctly define (function || function) inside a Javascript object?

From Dev

problems with retrieving the value of a class pointer object member allocated with operator new inside a function

From Dev

Class object inside or outside a parallel_for / parallel_for_each?

From Dev

How php requires a class inside a class function?

From Dev

C++ Static Function: Put Inside or Outside Scope of Class?

From Dev

PHP PDO: Search array value inside of MySQL's JSON object

From Dev

New issues in PDO Class

From Dev

PHP: Create new object of this class in class, is correct?

From Dev

Can I define a type inside a class in Object Pascal?

From Dev

Function which produces new variable for use outside of function - PHP

Related Related

  1. 1

    Function define a new object class

  2. 2

    PHP Call outside class function inside anonymous function

  3. 3

    Why are PHP tags not allowed inside a class but outside a function?

  4. 4

    PHP PDO injection inside a class

  5. 5

    How to define a member function outside class in python?

  6. 6

    Define a Function inside an Object with a variable as part of the function

  7. 7

    PHP function - works outside function, but not inside

  8. 8

    How to use an external variable inside a class/function/object in php

  9. 9

    Define member variables of class inside function

  10. 10

    function declaration inside object/class

  11. 11

    C++ - Define member function outside template-class but in header

  12. 12

    Can I define a private template function outside of a class?

  13. 13

    PHP pdo class getadresses warning and Call to a member function prepare() on a non-object

  14. 14

    Friend function inside class and outside class, what difference does it make?

  15. 15

    Define new function, array, struct etc inside of parameter of function call

  16. 16

    Define new function, array, struct etc inside of parameter of function call

  17. 17

    php public function is not working outside of the class

  18. 18

    Declare object outside function, pass it as reference and then create it inside function

  19. 19

    Why is a collection initializer without `new` allowed inside an object initializer but not outside?

  20. 20

    How to correctly define (function || function) inside a Javascript object?

  21. 21

    problems with retrieving the value of a class pointer object member allocated with operator new inside a function

  22. 22

    Class object inside or outside a parallel_for / parallel_for_each?

  23. 23

    How php requires a class inside a class function?

  24. 24

    C++ Static Function: Put Inside or Outside Scope of Class?

  25. 25

    PHP PDO: Search array value inside of MySQL's JSON object

  26. 26

    New issues in PDO Class

  27. 27

    PHP: Create new object of this class in class, is correct?

  28. 28

    Can I define a type inside a class in Object Pascal?

  29. 29

    Function which produces new variable for use outside of function - PHP

HotTag

Archive