Making a class method static and so be able to call from functions
Results 1 to 3 of 3

Thread: Making a class method static and so be able to call from functions

  1. #1
    phpMaster
    Join Date
    Jun 2005
    Location
    Europe
    Posts
    3,009

    Making a class method static and so be able to call from functions

    I have a working script, but I want to use static method in my class.
    There is a whole bunch of functions I use
    but I have to use database class handle
    as a 'global' in every function to be able to call the class.

    I want to make the method 'querySQL' static.
    And so I would be able to call it without using 'global'.

    I am certain there is a way. I would be glad to get help
    to modify my script.
    If it works from one function, it will work from all other functions.

    Here is what I got. The database class:
    PHP Code:
    class Database extends PDO
    {
        public function 
    __construct($dsn$dbuser ''$dbpass '')
        {
            try{
                
    parent::__construct($dsn$dbuser$dbpass);
            } catch (
    PDOException $e) {
                exit(
    'PDOException: '.$e->getMessage());
            }
        }

        public function 
    querySQL($sql)
        {
            
    $result $this->query($sql);
            if(
    $result === false) {
                echo 
    $this->errorInfo()[2].'<br>';
                exit(
    $sql);
            }
            return 
    $result;
        }

    And the function that uses the database class:
    PHP Code:
    function getPasswordHashById($id)
    {
        global 
    $pdo;

        
    $sql "SELECT passkey FROM users WHERE id=${id};";
        
    $result $pdo->querySQL($sql);
        return 
    $result->fetchColumn();
    }

    require(
    'Database.php');
    $pdo = new Database('sqlite:data/sqlitedata.db3');

    echo 
    getPasswordHashById(1); 
    Apache-2.4.28 (Win64) / PHP-7.1.10 / MySQL-5.7.19 / phpMyAdmin-4.7.4

  2. #2
    Senior Member
    Join Date
    Nov 2002
    Posts
    171
    A better bet is to pass the created database object as a parameter to the function that needs it. I also recommend using prepared statements to avoid the SQL injection security hole you currently have in this set-up.

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,544
    And perhaps many of these functions that use the database are pieces of an object that could then take the database connection as one of its properties.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •