Can I create a new object in __constructor() ? so then I can use the class in current class method.
Let say I have this class
class Config{
public function configure($data){
}
}
and I want to use Config
in some of Myclass
method like this :
include 'Config.php'
class Myclass {
function __construct(){
$this->conf = new Config(); //just create one config object
}
public function method1($data){
$this->conf->configure($data); //call the configure method
}
public function method2(){
$this->conf->configure($data); //call again the configure method
}
}
can I do like that above. or I must create new object frequently like this below:
class Myclass {
public function method1($data){
$this->conf = new Config(); //create config object
}
public function method2($data){
$this->conf = new Config(); //create again config object
}
}
Since I was new in writing my own php oop code,I would like to know which method are efficient when I want to create one object and used it in many function . Thanks!