How to use global classes that are not limited to class scope?

I started learning OOP in PHP. I managed to write code in which a subclass would extend a superclass that contains a database connection. Now, instead of extending or using a subclass, is there a way I can make this join class global so that any class can use its object without extending it?

Note that I must use $this->pdo to examine the class instance. Is there a way I can instantiate an object in this class, for example $pdo=new PDO(); , and use this object like $pdo wherever I want?

Would a static class help in this scenario?

 class connection { public $servername = "localhost"; public $username = "root"; public $password = ""; public $dbname = "carrental"; public $port="3306"; public $pdo; function addConnection() { try { $this->pdo = new PDO("mysql:host=$this->servername;port=$this->port;dbname=$this->dbname", $this->username, $this->password); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } $this->pdo->query("use $this->dbname"); } } 

I tried a singleton, as shown below, but can report what happened when I receive a fatal error and warning.

(!) Fatal error: in C: \ wamp \ www \ carRental \ index.php on line 20 (!)
PDOException: in C: \ wamp \ www \ carRental \ index.php on line 20 Call stack

Time Memory Function Location 1 0.0012 143752 {main} ()
.. \ index.php: 0 2 0.0012 144296 car → __ construct () .. \ index.php: 50

3 0.0013 144272 connection-> addConnection () .. \ index.php: 39
4 0.0989 150800 request () .. \ index.php: 20

 <?php class connection { public $servername = "localhost"; public $username = "root"; public $password = ""; public $dbname = "carrental"; public $port="3306"; public static $pdo; function addConnection() { try { self::$pdo = new PDO("mysql:host=$this->servername;port=$this->port;dbname=$this->dbname", $this->username, $this->password); self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } self::$pdo->query("use $this->dbname"); return self::$pdo; } } class car { public $name; public $maker; public $type; public $colour; public $passanger; public function __construct($param1,$param2,$param3,$param4,$param5) { $this->name=$param1; $this->maker=$param2; $this->type=$param3; $this->colour=$param4; $this->passanger=$param5; connection::addConnection(); } public function addCar() { $sql="INSERT INTO car(car_name,car_maker,car_type,car_colour,num_passanger)VALUES('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')"; $stmt = $this->$pdo->prepare($sql); $stmt->execute(); echo "Data inserted!"; } } $car1=new car("Honda Accord","Honda","5 wheeler","Red",8); $car1->addCar(); ?> 
+6
source share
1 answer

As I can see, this line causes connection::addConnection();
You are trying to call addConnection as a static method.
A static function means that you do not need to instantiate a class to call this function. But! when you call this static method, you cannot use non-static properties or a function of this class. Thus, all fields must be marked as static, because otherwise you will have no password, no username, no password.
TL: DR
Just mark "addConnection ()" as
public static function addConnection()
and you can use the static property / function of the class.

+1
source

All Articles