PHP 为什么我的扩展 class 中的全局数组是空的
PHP Why is my globals array empty in my extended class
我在扩展 class 中获取全局数组时遇到问题。该数组由使用 url 传递的参数填充。
我得到我的 url 然后爆炸它。然后我将前两个参数设置为 class 和方法。然后重新设置我的数组键,使参数从 0 开始。
例如:
class App
{
protected $controller = '';
protected $method = '';
protected $param = [];
public function init()
{
if (isset($_GET['url'])) {
$url = explode('/', filter_var(trim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
else
{
header('Location: /home');
}
if (file_exists(APP . '/controllers/' . $url[0] . '.php'))
{
$this->controller = $url[0];
unset($url[0]);
}
else
{
if (DEVELOPMENT == true) {
var_dump($url);
}
else
{
header('Location: /home');
}
}
require_once 'controllers/' . $this->controller . '.php';
$className = ucfirst($this->controller);
$class = new $className;
if (isset($url[1]))
{
if (method_exists($className, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
$methodName = $this->method;
$class->$methodName();
}
}
$GLOBALS['param'] = array_values($url);
}
public function view($view)
{
require_once VIEW . '/header.htm';
require_once VIEW . '/' . $view . '.htm';
require_once VIEW . '/footer.htm';
}
}
所以如果我的 url 是 site/class/method/foo/bar 并且我转储 $GLOBALS['param'] 我得到
array (size=2)
0 => string 'foo' (length=3)
1 => string 'bar' (length=3)
哪个好,哪个就是我想要的。
但是,如果我在我的扩展 class.
上做同样的转储
class User extends App
{
public function __construct()
{
var_dump($GLOBALS['param']);
}
}
我的数组是空的。实际上,我想要做的是能够从任何控制器 class 中的 url 获取参数并使用它们。我该怎么做?
我跑题了。我通过使用 call_user_func_array 将参数解析为方法来解决我的问题,就像这样
class App
{
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
public function init()
{
$url = $this->parseUrl();
if (file_exists(APP . '/controllers/' . $url[0] . '.php'))
{
$this->controller = $url[0];
unset($url[0]);
}
require_once APP . '/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if (isset($url[1]))
{
if (method_exists($this->controller, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
// Parse params to method
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl()
{
if (isset($_GET['url']))
{
return $url = explode('/', filter_var(trim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
public function view($view)
{
require_once VIEW . '/header.htm';
require_once VIEW . '/' . $view . '.htm';
require_once VIEW . '/footer.htm';
}
}
然后我可以在我的扩展中获取参数class
class User extends App
{
public function index($var = '', $otherVar = '')
{
echo $var. $otherVar;
}
public function register()
{
$this->view('user-register');
}
public function login()
{
$this->view('user-login');
}
}
我在扩展 class 中获取全局数组时遇到问题。该数组由使用 url 传递的参数填充。
我得到我的 url 然后爆炸它。然后我将前两个参数设置为 class 和方法。然后重新设置我的数组键,使参数从 0 开始。
例如:
class App
{
protected $controller = '';
protected $method = '';
protected $param = [];
public function init()
{
if (isset($_GET['url'])) {
$url = explode('/', filter_var(trim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
else
{
header('Location: /home');
}
if (file_exists(APP . '/controllers/' . $url[0] . '.php'))
{
$this->controller = $url[0];
unset($url[0]);
}
else
{
if (DEVELOPMENT == true) {
var_dump($url);
}
else
{
header('Location: /home');
}
}
require_once 'controllers/' . $this->controller . '.php';
$className = ucfirst($this->controller);
$class = new $className;
if (isset($url[1]))
{
if (method_exists($className, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
$methodName = $this->method;
$class->$methodName();
}
}
$GLOBALS['param'] = array_values($url);
}
public function view($view)
{
require_once VIEW . '/header.htm';
require_once VIEW . '/' . $view . '.htm';
require_once VIEW . '/footer.htm';
}
}
所以如果我的 url 是 site/class/method/foo/bar 并且我转储 $GLOBALS['param'] 我得到
array (size=2)
0 => string 'foo' (length=3)
1 => string 'bar' (length=3)
哪个好,哪个就是我想要的。
但是,如果我在我的扩展 class.
上做同样的转储class User extends App
{
public function __construct()
{
var_dump($GLOBALS['param']);
}
}
我的数组是空的。实际上,我想要做的是能够从任何控制器 class 中的 url 获取参数并使用它们。我该怎么做?
我跑题了。我通过使用 call_user_func_array 将参数解析为方法来解决我的问题,就像这样
class App
{
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
public function init()
{
$url = $this->parseUrl();
if (file_exists(APP . '/controllers/' . $url[0] . '.php'))
{
$this->controller = $url[0];
unset($url[0]);
}
require_once APP . '/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if (isset($url[1]))
{
if (method_exists($this->controller, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
// Parse params to method
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl()
{
if (isset($_GET['url']))
{
return $url = explode('/', filter_var(trim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
public function view($view)
{
require_once VIEW . '/header.htm';
require_once VIEW . '/' . $view . '.htm';
require_once VIEW . '/footer.htm';
}
}
然后我可以在我的扩展中获取参数class
class User extends App
{
public function index($var = '', $otherVar = '')
{
echo $var. $otherVar;
}
public function register()
{
$this->view('user-register');
}
public function login()
{
$this->view('user-login');
}
}