如何根据 php 中的数组值动态创建对象?
How can I create dynamically objects based on array values in php?
我想动态创建与我的 $instance 数组中存在的对象一样多的对象(例如 domain1_com
和 domain2_com
)并为它们指定数组值的名称(例如 domain1_com
和 domain2_com
) 所以我可以通过这些名称访问它(例如 domain1_com->example()
)。
可能吗?我尝试过类似的方法,但显然不起作用。
class myClass
{
public static function getInstances()
{
// I connect to the database and execute the query
$sql = "SELECT * FROM my_table";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
if ($stmt->execute()) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Read values in my array
foreach ($results as $instance) {
$obj = $instance["domain"]);
// return 2 values: domain1_com and domain2_com
$obj = new myClass();
}
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
$domain1_com->example();
$domain2_com->example();
可以使用可变变量。
$name = "foo";
$$name = new bar();
与
相同
$foo = new bar();
您无法在该方法之外访问在 getInstances
中创建的变量。它们是本地的,而不是全球的。
试试这个代码:
class myClass
{
public static function getInstances()
{
$results = array('domain1_com', 'domain2_com');
foreach ($results as $instance) {
$$instance = new myClass();
// This is equivalent to "$domainX_com = new myClass();".
// Writing that code here would define a _local_ variable named 'domainX_com'.
// This being a method inside a class any variables you define in here are _local_,
// so you can't access them' _outside_ of this method ('getInstances')
}
// this will work, we are inside 'getInstances'
$domain1_com->example();
$domain2_com->example();
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
// this won't work. $domainX_com are not accessible here. they only exist _inside_ 'getInstances'
// It's basic OOP.
// so this code will crash
$domain1_com->example();
$domain2_com->example();
它将产生这个输出:
This is an instance
This is an instance
E_NOTICE : type 8 -- Undefined variable: domain1_com -- at line 32
E_ERROR : type 1 -- Call to a member function example() on a non-object -- at line 32
您需要一种方法来访问这些变量。我会用这个:
class myClass
{
private static $instances = array();
public static function getInstances()
{
$results = array('domain1_com', 'domain2_com');
foreach ($results as $instanceName) {
self::$instances[$instanceName] = new myClass();
}
}
public static function getInstance($instanceName) {
return self::$instances[$instanceName];
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
// this will work
myClass::getInstance('domain1_com')->example();
myClass::getInstance('domain2_com')->example();
我想动态创建与我的 $instance 数组中存在的对象一样多的对象(例如 domain1_com
和 domain2_com
)并为它们指定数组值的名称(例如 domain1_com
和 domain2_com
) 所以我可以通过这些名称访问它(例如 domain1_com->example()
)。
可能吗?我尝试过类似的方法,但显然不起作用。
class myClass
{
public static function getInstances()
{
// I connect to the database and execute the query
$sql = "SELECT * FROM my_table";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
if ($stmt->execute()) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Read values in my array
foreach ($results as $instance) {
$obj = $instance["domain"]);
// return 2 values: domain1_com and domain2_com
$obj = new myClass();
}
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
$domain1_com->example();
$domain2_com->example();
可以使用可变变量。
$name = "foo";
$$name = new bar();
与
相同$foo = new bar();
您无法在该方法之外访问在 getInstances
中创建的变量。它们是本地的,而不是全球的。
试试这个代码:
class myClass
{
public static function getInstances()
{
$results = array('domain1_com', 'domain2_com');
foreach ($results as $instance) {
$$instance = new myClass();
// This is equivalent to "$domainX_com = new myClass();".
// Writing that code here would define a _local_ variable named 'domainX_com'.
// This being a method inside a class any variables you define in here are _local_,
// so you can't access them' _outside_ of this method ('getInstances')
}
// this will work, we are inside 'getInstances'
$domain1_com->example();
$domain2_com->example();
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
// this won't work. $domainX_com are not accessible here. they only exist _inside_ 'getInstances'
// It's basic OOP.
// so this code will crash
$domain1_com->example();
$domain2_com->example();
它将产生这个输出:
This is an instance
This is an instance
E_NOTICE : type 8 -- Undefined variable: domain1_com -- at line 32
E_ERROR : type 1 -- Call to a member function example() on a non-object -- at line 32
您需要一种方法来访问这些变量。我会用这个:
class myClass
{
private static $instances = array();
public static function getInstances()
{
$results = array('domain1_com', 'domain2_com');
foreach ($results as $instanceName) {
self::$instances[$instanceName] = new myClass();
}
}
public static function getInstance($instanceName) {
return self::$instances[$instanceName];
}
public function example()
{
echo "This is an instance";
}
}
myClass::getInstances();
// this will work
myClass::getInstance('domain1_com')->example();
myClass::getInstance('domain2_com')->example();