——-Now, another question, can anyone explain?
class DBFactory {
static function create(){
return new MysqlDB();
}
}
class MysqlDB{
function alo(){
echo 'ok';
}
}
$db = DBFactory::create();
$db->alo();
—>Works
class DBFactory {
function create(){
return new MysqlDB();
}
}
class MysqlDB{
function alo(){
echo 'ok';
}
}
$db = new DBFactory;
$db->create();
$db->alo();
—>Not works
Because you’re returning a new
MysqlDBinstance, but are not saving the reference anywhere.DBFactorydoes not have analomethod. Compare: