对多个输出使用变量? [ PHP 函数 ]
Use variables for more than one output? [ PHP Functions ]
我目前是一名初级开发人员,刚开始我的第一个大项目,我有空闲时间,我想做的基本上是将变量写入 html/tpl 文档,我目前有开始工作,这是我的代码:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username', $username); // $username Will be replaced by database queried results once completed.
}
这里是 setParams 函数。
function setParams($item1, $item2){
ob_start();
$theme = 'default';
include_once T . '/'.$theme.'/index.php'; // T . is defined at the beginning of the document.
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
echo preg_replace('~{(['.$item1.']*)}~i', ''.$item2.'', $html, 1);
}
}
这里是 html/tpl 文档中的编码。
{username} has been online for {onlineTime} Hours
对于你们中的一些人来说,这可能是一个非常简单的代码,但由于这是我的第一次尝试,所以我只能做到这一点。
我想做的是拥有它,这样您就可以根据需要多次设置参数,而无需像这样更改 $ 变量名称:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username',$username);
$this->setParams('OnlineTime', $onlineTime);
}
同时保持 setParams($item1, $item2)
但是正如您想象的那样,这只是完全削减了代码。有谁知道这个问题的解决方案?我整天都在寻找,但没有任何真正的运气。
提前致谢,
拉尔夫
我想你需要的是一个带有静态方法的class;
<?php
class Params {
public static $params = array();
public static function setParam($key, $value) {
self::$params[$key] = $value;
}
public static function getParam($key) {
if (isset(self::$params[$key])) {
return self::$params[$key];
}
}
}
// Usage
// Set Username
Params::setParam("username", "JohnDoe");
Params::setParam("password", "12345");
echo Params::getParam("username");
echo Params::getParam("password");
我目前是一名初级开发人员,刚开始我的第一个大项目,我有空闲时间,我想做的基本上是将变量写入 html/tpl 文档,我目前有开始工作,这是我的代码:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username', $username); // $username Will be replaced by database queried results once completed.
}
这里是 setParams 函数。
function setParams($item1, $item2){
ob_start();
$theme = 'default';
include_once T . '/'.$theme.'/index.php'; // T . is defined at the beginning of the document.
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
echo preg_replace('~{(['.$item1.']*)}~i', ''.$item2.'', $html, 1);
}
}
这里是 html/tpl 文档中的编码。
{username} has been online for {onlineTime} Hours
对于你们中的一些人来说,这可能是一个非常简单的代码,但由于这是我的第一次尝试,所以我只能做到这一点。
我想做的是拥有它,这样您就可以根据需要多次设置参数,而无需像这样更改 $ 变量名称:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username',$username);
$this->setParams('OnlineTime', $onlineTime);
}
同时保持 setParams($item1, $item2)
但是正如您想象的那样,这只是完全削减了代码。有谁知道这个问题的解决方案?我整天都在寻找,但没有任何真正的运气。
提前致谢,
拉尔夫
我想你需要的是一个带有静态方法的class;
<?php
class Params {
public static $params = array();
public static function setParam($key, $value) {
self::$params[$key] = $value;
}
public static function getParam($key) {
if (isset(self::$params[$key])) {
return self::$params[$key];
}
}
}
// Usage
// Set Username
Params::setParam("username", "JohnDoe");
Params::setParam("password", "12345");
echo Params::getParam("username");
echo Params::getParam("password");