php - How to use global classes which is not limited to a class scope? -
i've started learning oop in php. managed write code subclass extend superclass contains connection database. instead of extending or using subclass, there way can make connection class global class use it's object without having extend it?
please note below, have use $this->pdo regard instance of class. there way can instantiate object within class $pdo=new pdo(); , use object $pdo wherever want?
will static class in 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 below can advise what's wrong fatal error , 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 stacktime memory function location 1 0.0012 143752 {main}( )
..\index.php:0 2 0.0012 144296 car->__construct( ) ..\index.php:503 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 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(); ?>
as can see line cause problems connection::addconnection();
you trying call addconnection static method.
static function means don't need create instance of class call function. !but! when call method static, can't use non-static properties or function of class. of fields should marked static, because otherwise not have of db password, login , on.
tl:dr
mark "addconnection()"
public static function addconnection()
, can use static property/function of class.
Comments
Post a Comment