0
 class Conn           // No parent db class - it serves no purpose
                         // Class names begin with capitals, by convention
    {
    
        private $host;   // Local copies of database credentials. Not really needed
        private $user;
        private $pass;
        private $dbname;
    
        // Set up the database connection in the constructor.
        public function __construct(string $host, string $user, string $password, string $dbname) {
            $this->host = $host;
            $this->user = $user;
            $this->pass = $password;
            $this->dbname = $dbname;
    
            // Set mysqli to throw exceptions on error
            mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

            // open a connection for later use
            $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        }
    }
    
    class Work extends Conn
    {
    
        public function insert_user(string $email, string $password_hash)
        {
            $sql = "INSERT INTO user (email,password) VALUES (?,?)";
            $stmt = $this->conn->prepare($sql);
            $stmt->bind_param('ss', $email, $password_hash);
            $stmt->execute();
            return;
        }
    }
    
    
    
    try {
        $obj = new Work("server", "username", "password", "schema");
        $obj->insert_user('user@example.com', 'asdfasdf');
        echo "inserted";
    } catch (Exception $e) {
        echo $e->getMessage();
        }

I have this code in class.php file . How can I use this page $obj in another file. Like I want to use $obj in prcess.php file. How can I do it now?

zmag
  • 6,257
  • 12
  • 26
  • 33
  • You would need to include this file in the other one. – El_Vanja Apr 29 '21 at 16:26
  • I included class file in process file but $obj not detecting. – user7252501 Apr 29 '21 at 16:27
  • Then there's something you've done wrong or you've represented something incorrectly in your question. This code snippet in the question, is all this code in the class file (including that `try - catch` block)? – El_Vanja Apr 29 '21 at 16:33
  • try { $obj = new Work("server", "username", "password", "schema"); $obj->insert_user('user@example.com', 'asdfasdf'); echo "inserted"; } catch (Exception $e) { echo $e->getMessage(); } – user7252501 Apr 29 '21 at 16:36
  • only $obj in try catch block – user7252501 Apr 29 '21 at 16:37
  • My question was is that piece of code inside the same file as these classes? – El_Vanja Apr 29 '21 at 16:40
  • yes in same file – user7252501 Apr 29 '21 at 16:40
  • If it's in the same file and you correctly include that file in the processing file, this variable should be available there. Is PHP reporting any errors, warnings or notices when you include it? Check the error log or [display errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) during development. – El_Vanja Apr 29 '21 at 16:44

0 Answers0