I want to know how we could use a whole object inside an another object without using inheritance(if it is possible). if not how could we use PDO class with MySQL Database inherit with a class named abc having a default constructor. how could i use it manage to use PDO constructor value and abc constructor value at the same time?
here are the two objects
<?php $pdo=new PDO('mysql:dbname=database;host=localhost','root','pwd'); class abc { var $a, $b; function __construct($parm1, $pram2) { $this->a=$parm1; $this->b=$parm2; } } ?>
In PHP an object and a class are essentially the same thing. The __construct
function is called when you create a new class object and you can pass arguments into the function at that time like so:
class abc {
function __construct($dsn, $user, $password) {
$this->pdo = new PDO($dsn, $user, $password);
}
}
$my_class = new abc('mysql:dbname=database;host=localhost','root','pwd');
But I would do something like this to keep it simple:
class abc {
function __construct() {
$this->pdo = new PDO('mysql:dbname=database;host=localhost','root','pwd');
}
}
$my_class = new abc();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.