[英]How to pass variable from outside class to class function in php
i have an php class for to set cron jobs on php, all is working fine but at last step i get stack by passing variable inside class.So my question is how to pass a variable from outside class to inside class function?我有一个 php 类用于在 php 上设置 cron 作业,一切正常但最后一步我通过在类内部传递变量来获取堆栈。所以我的问题是如何将变量从外部类传递到内部类函数? I tried to set variable before class but still not get any results.
我试图在课前设置变量,但仍然没有得到任何结果。 My class is:
我的课是:
namespace Rumd3x\Scheduler;
use Carbon\Carbon;
use Closure;
use Cron\CronExpression;
use Exception;
class Schedule
{
const LOCK_FILE = 'scheduler.lock';
private $action;
private $name = "Default";
private $lock = null;
private $cron = [];
public function __destruct()
{
$this->free();
}
public function run()
{
$valid_action = ($this->action instanceof Closure);
if ($this->getLock() && $valid_action) {
try {
$cron = $this->makeCron();
$retorno = $this->eval($cron);
} catch (Exception $e) {
$retorno = false;
} finally {
$this->free();
return $retorno;
}
}
return false;
}
function eval(CronExpression $cron) {
if ($cron->isDue()) {
$now_str = Carbon::now()->format('d/m/Y H:i:s');
print "[$now_str]: Job {$this->name} Started\n";
$closure = $this->action;
$retorno = $closure($cron);
$now_str = Carbon::now()->format('d/m/Y H:i:s');
print "[$now_str]: Job {$this->name} Finished OK\n";
} else {
$retorno = false;
}
return $retorno;
}
private function getLock()
{
if (!file_exists(self::LOCK_FILE)) {
file_put_contents(self::LOCK_FILE, '');
}
chmod(self::LOCK_FILE, 0777);
$this->lock = fopen('./' . self::LOCK_FILE, 'r+');
if (!$this->lock) {
throw new Exception("Job {$this->name} Failed: Error Opening Lock File\n");
}
if (!flock($this->lock, LOCK_EX)) {
throw new Exception("Job {$this->name} Failed: Error Obtaining Lock on Resource\n");
}
return true;
}
private function free()
{
if ($this->lock && is_resource($this->lock) && get_resource_type($this->lock) !== 'Unknown') {
flock($this->lock, LOCK_UN);
fclose($this->lock);
}
return $this;
}
private function makeCron()
{
$parsed = [];
for ($i = 0; $i <= 4; $i++) {
$parsed[$i] = isset($this->cron[$i]) ? $this->cron[$i] : '*';
}
$expression = implode(' ', $parsed);
return CronExpression::factory($expression);
}
public function isDue()
{
return $this->makeCron()->isDue();
}
public static function action(Closure $call)
{
$instance = new static;
$instance->action = $call;
return $instance;
}
public function cron(String $expression)
{
$this->cron = explode(' ', $expression);
return $this;
}
public function at(String $time)
{
try {
$time = explode(':', $time);
$this->cron[1] = $time[0]; // Hours
$this->cron[0] = $time[1]; // Minutes
} catch (\Exception $e) {
$this->cron[1] = '*';
$this->cron[0] = '*';
} finally {
return $this;
}
}
public function monthly(Int $day = 1)
{
$day = in_array($day, range(1, 31)) ? $day : 1;
$this->cron[0] = '0';
$this->cron[1] = '0';
$this->cron[2] = $day;
return $this;
}
public function weekly(Int $weekday = 0)
{
$weekday = in_array($weekday, range(0, 6)) ? $weekday : 0;
$this->cron = ['0', '0', '*', '*', $weekday];
return $this;
}
public function daily()
{
$this->cron = ['0', '0', '*', '*', '*'];
return $this;
}
public function hourly(Int $minute = 0)
{
$minute = in_array($minute, range(0, 59)) ? $minute : 1;
$this->cron = [$minute, '*', '*', '*', '*'];
return $this;
}
public function everyThirtyMinutes()
{
$this->cron = ['*/30', '*', '*', '*', '*'];
return $this;
}
public function everyFifteenMinutes()
{
$this->cron = ['*/15', '*', '*', '*', '*'];
return $this;
}
public function everyTenMinutes()
{
$this->cron = ['*/10', '*', '*', '*', '*'];
return $this;
}
public function everyFiveMinutes()
{
$this->cron = ['*/5', '*', '*', '*', '*'];
return $this;
}
public function everyMinute()
{
$this->cron = ['*', '*', '*', '*', '*'];
return $this;
}
public function setName(String $name)
{
$this->name = $name;
return $this;
}
}
And i call the class on this way:我这样称呼班级:
require_once __DIR__ . './vendor/autoload.php';
use Cron\CronExpression;
use Rumd3x\Scheduler\Schedule;
$getTasks = $database->select("SELECT * FROM crons");
while($row = $database->fetch($getTasks)){
Schedule::action(function() {
exec("php -f C:/xampp/".$row['c_file']." 2>&1");
})->cron(''.$row['c_frequency'].'')->run();
}
The problem is that $row['c_file'] and $row['c_frequency'] not working inside class function... how can do this?问题是 $row['c_file'] 和 $row['c_frequency'] 不能在类函数内部工作……这怎么办? I tried also to call a function from outside while with variables but still not working.
我还尝试在使用变量时从外部调用一个函数,但仍然无法正常工作。
Thank you!谢谢!
Change from改变自
while($row = $database->fetch($getTasks)){
Schedule::action(function() {
exec("php -f C:/xampp/".$row['c_file']." 2>&1");
})->cron(''.$row['c_frequency'].'')->run();
}
To到
while($row = $database->fetch($getTasks)){
Schedule::action(function() use($row) { // <-- here used the "use" keyword
exec("php -f C:/xampp/".$row['c_file']." 2>&1");
})->cron(''.$row['c_frequency'].'')->run();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.