我们已经开始与 PHP 开发人员合作,我查看了他的代码,从我的角度来看,写这样的东西是浪费墨水。我个人不做OOP。
他从来没有回答过这个问题,擅长OOP的人可以解释一下。
在文件inc/path.php中,您为文件和文件夹的路径定义了具有不同变量的类 Path
class Path
{
var $path_cnfg = null;
/* URLs */
var $url_css = '';//for css url
var $url_js = '';//for javascript url
var $url_img = '';//for image url
/* Paths */
var $path_inc = '';//inc directory path
var $path_img = '';//image path
function Path()
{
$this->path_cnfg = new Config();
/* Setting URL(s) */
$this->url_css = $this->path_cnfg->base_url.'assets/css/';
$this->url_js = $this->path_cnfg->base_url.'assets/js/';
$this->url_img = $this->path_cnfg->base_url.'assets/img/';
/* Setting Path(s) */
$this->path_inc = $this->path_cnfg->root_path.'/inc/';
$this->path_img = $this->path_cnfg->root_path.'/img/';
}
}
A. 类路径中的方法路径的目的是什么?
function Path()
{
$this->path_cnfg = new Config();
/* Setting URL(s) */
$this->url_css = $this->path_cnfg->base_url.'assets/css/';
$this->url_js = $this->path_cnfg->base_url.'assets/js/';
$this->url_img = $this->path_cnfg->base_url.'assets/img/';
/* Setting Path(s) */
$this->path_inc = $this->path_cnfg->root_path.'/inc/';
$this->path_img = $this->path_cnfg->root_path.'/img/';
}
B. 在obj.php中,您将变量从对象$path传输到数组$data。
$data['path_img'] = $path->path_img;//Assign image directory path
$data['path_root'] = $config->root_path;//Assign root path
$data['url_base'] = $config->base_url;//Assign base url
$data['url_css'] = $path->url_css;//Assign css directory path
$data['url_js'] = $path->url_js;//Assign javascript directory path
$data['url_img'] = $path->url_img;//Assign url for image
让类Path自己定义数组$data并直接使用它的目的是什么?为什么我们需要通过所有这些方法和对象来获取我们最初可以在 6 行代码中定义的相同变量($data)。例如
$data["path_img"] = $config->base_url . "assets/img/";
...
$data["url_css"] = $config->base_url . "/assets/css/"
$data["url_js"] = $config->base_url . "assets/js/"
$data["url_img"] = $config->base_url . "assets/img/"