PHP 将参数动态传递给 class 的构造函数
PHP passing arguments into class's constructor dynamically
我在我的项目框架中写了一个 PHP class,它包含一个构造函数,为了这个 class 它包含一个名为 [=15= 的参数].
我的 class 作为我正在构建的功能的一部分动态加载,我需要从数组中将一个值加载到我的参数中,但是当我这样做时,它只是通过 Array
即使我使用 array_values
,例如,这是我的 class:
<?php
class GreetingJob
{
/**
* The name
*/
public $name;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Write data to a file
*/
public function writeToFile($data = '')
{
$file = fopen('000.txt', 'w');
fwrite($file, $data);
fclose($file);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$this->writeToFile('Hello ' . $this->name);
} catch (Exception $e) {
// do something
}
}
}
加载位置:
/**
* Get the job
*/
public function getJob($job = '') {
return APP . "modules/QueueManagerModule/Jobs/$job.php";
}
/**
* Check that the job exists
*/
public function jobExists($job = '') {
if (!file_exists($this->getJob($job))) {
return false;
}
return true;
}
/**
* Execute the loaded job
*/
public function executeJob($class, $rawJob = [], $args = []) {
require_once $this->getJob($class);
$waitTimeStart = strtotime($rawJob['QueueManagerJob']['available_at']) / 1000;
$runtimeStart = microtime(true);
// initiate the job class and invoke the handle
// method which runs the job
$job = new $class(array_values(unserialize($args)));
$job->handle();
}
$args
看起来像这样:
[
'name' => 'john'
]
如何按参数出现的顺序将参数动态传递到我的 class 并使用每个参数的值。
这是在 php 中使用反射动态实例化 class 的方法:
$className = 'GreetingJob';
$args = [];
$ref = new \ReflectionClass($className);
$obj = $ref->newInstanceArgs($args);
https://www.php.net/manual/en/reflectionclass.newinstanceargs.php
array_values() 还是 returns 一个数组。它所做的一切都是将键重置为连续的 zero-based 整数。
我想你想使用 splat operator:
$job = new $class(...array_values(unserialize($args)));
完整的可运行示例:
<?php
class GreetingJob
{
public function __construct($name)
{
var_dump($name);
}
}
$class = 'GreetingJob';
$args = serialize(
[
'name' => 'Jimmy',
]
);
$job = new $class(...array_values(unserialize($args)));
请注意,整体设计可能会令人困惑。在关联数组中接受参数表明名称很重要而位置不重要,但恰恰相反。
我在我的项目框架中写了一个 PHP class,它包含一个构造函数,为了这个 class 它包含一个名为 [=15= 的参数].
我的 class 作为我正在构建的功能的一部分动态加载,我需要从数组中将一个值加载到我的参数中,但是当我这样做时,它只是通过 Array
即使我使用 array_values
,例如,这是我的 class:
<?php
class GreetingJob
{
/**
* The name
*/
public $name;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Write data to a file
*/
public function writeToFile($data = '')
{
$file = fopen('000.txt', 'w');
fwrite($file, $data);
fclose($file);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$this->writeToFile('Hello ' . $this->name);
} catch (Exception $e) {
// do something
}
}
}
加载位置:
/**
* Get the job
*/
public function getJob($job = '') {
return APP . "modules/QueueManagerModule/Jobs/$job.php";
}
/**
* Check that the job exists
*/
public function jobExists($job = '') {
if (!file_exists($this->getJob($job))) {
return false;
}
return true;
}
/**
* Execute the loaded job
*/
public function executeJob($class, $rawJob = [], $args = []) {
require_once $this->getJob($class);
$waitTimeStart = strtotime($rawJob['QueueManagerJob']['available_at']) / 1000;
$runtimeStart = microtime(true);
// initiate the job class and invoke the handle
// method which runs the job
$job = new $class(array_values(unserialize($args)));
$job->handle();
}
$args
看起来像这样:
[
'name' => 'john'
]
如何按参数出现的顺序将参数动态传递到我的 class 并使用每个参数的值。
这是在 php 中使用反射动态实例化 class 的方法:
$className = 'GreetingJob';
$args = [];
$ref = new \ReflectionClass($className);
$obj = $ref->newInstanceArgs($args);
https://www.php.net/manual/en/reflectionclass.newinstanceargs.php
array_values() 还是 returns 一个数组。它所做的一切都是将键重置为连续的 zero-based 整数。
我想你想使用 splat operator:
$job = new $class(...array_values(unserialize($args)));
完整的可运行示例:
<?php
class GreetingJob
{
public function __construct($name)
{
var_dump($name);
}
}
$class = 'GreetingJob';
$args = serialize(
[
'name' => 'Jimmy',
]
);
$job = new $class(...array_values(unserialize($args)));
请注意,整体设计可能会令人困惑。在关联数组中接受参数表明名称很重要而位置不重要,但恰恰相反。