Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use global classes which is not limited to a class scope?

Tags:

oop

php

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

Please note below, I have to use $this->pdo to regard the instance of the class. Is there a way where I can instantiate an object within this class like $pdo=new PDO(); and use this object as $pdo wherever I want?

Will 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");
    }
}

Tried Singleton like below but can advise what's wrong as I get 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 query ( ) ..\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();

?>
like image 417
112233 Avatar asked Jul 07 '15 09:07

112233


1 Answers

As i can see this line cause problems connection::addConnection();
You are trying to call addConnection like static method.
Static function means that you don't need to create instance of class to call this function. !But! when you call this method static, You can't use non-static properties or function of this class. So all of fields should be marked as static, because otherwise you will not have any of db password, login and so on.
TL:DR
Just mark "addConnection()" as
public static function addConnection()
and you can use static property/function of class.

like image 192
Warzyw Avatar answered Nov 12 '22 22:11

Warzyw