1 回答
TA贡献1772条经验 获得超5个赞
当你实例化变量时,它们有一定的范围,在这个范围内变量是已知的。我不明白你是如何加载文件的,但我假设你希望在访问 index.php 时发生 transaction.php 的逻辑。要实现此目的,可以将 transaction.php 放入一个以 $errors 作为构造函数变量的类,实例化它并调用该方法:
[交易.php]
class Transaction
{
private $errors;
public function _construct($errors)
{
$this->errors = $errors;
}
public function handleErrors()
{
if(is_array($errors))
{
echo 'something';
}
else
{
echo 'something';
}
}
}
[getTransaction.php]
Class Template
{
public function __construct($template, $vars)
{
$this->template = $template;
$this->vars = $vars;
$transaction = new Transaction($vars); //instantiates your class
$transaction->handleErrors(); //calls the method
}
public function __toString()
{
foreach ($this->vars as $name => $value)
{
$name = $value;
}
unset($name, $value);
ob_start();
require('templates/' . $this->template . '.php');
return ob_get_clean();
}
}
或者将 transaction.php 变成您在 index.php 中定义并调用的函数: [index.php]
function checkTransaction()
{
if(!empty($_POST['code']))
{
return true;
}
return [
'code' => false
];
}
function handleErrors($errors)
{
if(is_array($errors))
{
echo 'something';
}
else
{
echo 'something';
}
}
function renderTransactionResult($errors)
{
$getTemplate = new Template('transaction', [
'errors' => $errors
]);
echo $getTemplate;
}
$transactionid = htmlspecialchars($_POST['search']);
$transactionrows = $main->CheckNumRowsTransaction($transactionid);
if($transactionrows === 1)
{
$errors = checkTransaction($_POST);
renderTransactionResult($errors);
}
else
{
$errors = [
'transaction' => false
];
renderTransactionResult($errors);
}
请注意,我没有审查您的功能、效率或安全性代码,而只是制定了解决您的范围问题的答案。
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报