【发布时间】:2014-03-26 18:05:39
【问题描述】:
我正在尝试在我的 Web 应用程序的“全局范围”中创建我的类变量,以便在您调用它们时可以轻松地在其他类中使用它们并在整个 Web 应用程序中使用它们。这是我的 Web 应用程序的 Articles 类。
class Articles {
// Defined variables that constructs an Article
private $id
private $title
private $summary
private $content
private $author
public function __construct($id, $title, $summary, $content, $author) {
// Constructs our Article by default
$this->id = $id;
$this->title = $title;
$this->summary = $summary;
$this->content = $content;
$this->author = $author;
}
}
这是我的 init.php 文件
// Require the Articles and ArticlesHandler Class
require 'Articles/Articles.php';
require 'Articles/ArticlesHander.php';
如果我需要在 ArticlesHandler 中调用 $title,它是否可以只使用 $title 或者我需要它使用 $this->title 来调用它?还是有更好的方法来做到这一点?
【问题讨论】:
-
这里实际上没有创建任何类实例......不要将类定义与类变量混淆(除非您指的是类实例,否则没有这样的东西)并了解自动加载器而不是处理包含为您的 init.php 文件中的所有内容
-
我在文件中调用类的实例。 init 是 Db、文章、成员等的全部内容。该实例在 index.php 中调用。很抱歉造成混乱。
-
@Traven 您能否编辑您的问题以显示创建实例的文件(例如
$article = new Article(1, 'Hello', 'World', '!', 'me');)
标签: php class constructor