parent::__construct() 发送的方法中的访问值
Access value in method sent by parent::__construct()
我在控制器的子 class 中获取通过 parent__construct 传递的值并在名为 View 的 class 中使用该值时遇到问题。
在控制器 __Construct 中执行 var_dump 时 class 我可以检索从表单 class 发送的值,但是当我尝试检索此值时视图内方法中的相同值 class,它显示为 null。
你能帮我解决这个问题吗?
代码如下:
============================================
class Form extends Controller
{
//General builder of the WEB pages
public function __construct()
{
//Redirecting the entire site for maintenance
//redirect("/ops/maintenance");
parent::__construct(__DIR__ . "/../../../themes/" . THEME . "/pages/");
}
}
============================================
class controller
{
/** @var View */
protected $view;
/** @var Message */
protected $message;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
//the value passed in parent::__construct() appears here
var_dump($pathToViews);
$this->message = new Message();
}
}
============================================
//Class responsible for handling Views requests
class view
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
更新:
如果你不想在控制器 class 中调用 show 方法,但你想在你的控制器 class 中实例化 class 你应该找到一种方法来让视图 class 可以在控制器 class 之外访问。只是在控制器外部再次实例化视图 class 是另一个实例,并且确实会在 $pathToViews 变量上显示空值。
一种方法是这样的:
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
var_dump($pathToViews);
}
public function getView() : View
{
return $this->view;
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
echo $this->pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$controller = new controller('testString');
$view = $controller->getView();
$view->show('test');
另一种方法是使用依赖倒置原则
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param View $view
* @param string|null $pathToViews
*/
public function __construct(View $view, string $pathToViews = null)
{
$this->view = $view;
$this->view->setPathtoViews($pathToViews);
var_dump($pathToViews);
}
public function getView() : View
{
return $this->view;
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function setPathtoViews(string $pathToViews)
{
$this->pathToViews = $pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$view = new View();
$controller = new controller($view, 'testString');
$view->show('test');
?>
原回答
你的代码 运行 在我的机器上没问题。我有 运行 下面的代码块。我所做的更改使其工作实际上是调用 $this->view->show()。在您的代码示例中,情况并非如此。
作为小记。我知道 php 不区分大小写,但作为一个好习惯,请使用与您定义它们的方式相同的大小写调用您的 classes(视图与视图)。
<?php
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
$this->view->show('test');
var_dump($pathToViews);
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
echo $this->pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$controller = new controller('testString');
?>
我在控制器的子 class 中获取通过 parent__construct 传递的值并在名为 View 的 class 中使用该值时遇到问题。
在控制器 __Construct 中执行 var_dump 时 class 我可以检索从表单 class 发送的值,但是当我尝试检索此值时视图内方法中的相同值 class,它显示为 null。
你能帮我解决这个问题吗?
代码如下:
============================================
class Form extends Controller
{
//General builder of the WEB pages
public function __construct()
{
//Redirecting the entire site for maintenance
//redirect("/ops/maintenance");
parent::__construct(__DIR__ . "/../../../themes/" . THEME . "/pages/");
}
}
============================================
class controller
{
/** @var View */
protected $view;
/** @var Message */
protected $message;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
//the value passed in parent::__construct() appears here
var_dump($pathToViews);
$this->message = new Message();
}
}
============================================
//Class responsible for handling Views requests
class view
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
更新:
如果你不想在控制器 class 中调用 show 方法,但你想在你的控制器 class 中实例化 class 你应该找到一种方法来让视图 class 可以在控制器 class 之外访问。只是在控制器外部再次实例化视图 class 是另一个实例,并且确实会在 $pathToViews 变量上显示空值。
一种方法是这样的:
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
var_dump($pathToViews);
}
public function getView() : View
{
return $this->view;
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
echo $this->pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$controller = new controller('testString');
$view = $controller->getView();
$view->show('test');
另一种方法是使用依赖倒置原则
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param View $view
* @param string|null $pathToViews
*/
public function __construct(View $view, string $pathToViews = null)
{
$this->view = $view;
$this->view->setPathtoViews($pathToViews);
var_dump($pathToViews);
}
public function getView() : View
{
return $this->view;
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function setPathtoViews(string $pathToViews)
{
$this->pathToViews = $pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$view = new View();
$controller = new controller($view, 'testString');
$view->show('test');
?>
原回答 你的代码 运行 在我的机器上没问题。我有 运行 下面的代码块。我所做的更改使其工作实际上是调用 $this->view->show()。在您的代码示例中,情况并非如此。
作为小记。我知道 php 不区分大小写,但作为一个好习惯,请使用与您定义它们的方式相同的大小写调用您的 classes(视图与视图)。
<?php
class controller
{
/** @var View */
protected $view;
//Builder extending classes
/**
* Controller constructor
* @param string|null $pathToViews
*/
public function __construct(string $pathToViews = null)
{
$this->view = new View($pathToViews);
$this->view->show('test');
var_dump($pathToViews);
}
}
//Class responsible for handling Views requests
class View
{
protected $pathToViews;
public function __construct(string $pathToViews = null)
{
$this->pathToViews = $pathToViews;
echo $this->pathToViews;
}
//Function to Load the View and send content from the backend to the frontend
public function show($viewName, $data = [])
{
var_dump($this->pathToViews);
}
}
$controller = new controller('testString');
?>