【发布时间】:2016-04-22 20:45:24
【问题描述】:
我有一个代码('require_once' 部分无关紧要):
<?php
require_once('../shared/database.php');
class Administration{
public $db;
public $base;
public function __construct(){
$this->db=new Database();
$this->base=new stdClass();
$this->base->url="http://".$_SERVER ['SERVER_NAME'].'/phpp/blog/admin/';
}
}
?>
下面写的代码正在实现代码(上面写的)。
<?php
require_once('administration.php');
class Articles extends Administration{
public function __construct(){
parent::__construct();}
$announcement='Some text.';
header('Location: '.$this->base->db.'articles.php?announcement='.urlencode($announcement));}
?>
有问题的部分是$this->base->db.
如果我将 db 变量名更改为任何其他有效的变量名,代码仍然有效,但我无法找出原因。
有人知道它是如何工作的吗?
【问题讨论】:
-
$this->base->db是一个对象。除非你有__toString方法,否则将对象转换为字符串是很奇怪的。通过输出db属性,您期望什么? -
如果你将url存储在
$this->base->url中,为什么还要使用$this->base->db?你知道你的代码发生了什么吗? -
您在
$announcement行缺少开场白。 -
致 u_mulder:我知道 db 是一个对象,但 db(对象)是 $this->db 变量,我写了 $this->base->db。您也可以使用 $this->base->XYZ ,它将使用存储在 $this->base->url 中的值。
-
U_mulder 的第二个帖子回复:我永远不会使用它。我在学习书中找到了它,但我找不到它是如何工作的。
标签: php this variable-assignment