PHP:静态函数的使用
我有一个愚蠢的问题。 我一直在尝试编写我的框架,但是......我错过了一些东西。 我有一个基类 Base.class.php,具有一些功能。 当我编写另一个扩展 Base 的类 SubBase.class.php 时,我尝试设置一个必须在 Base 类中使用的变量,在一个静态函数中(可能)。 类似的东西
class Base {
public $vars;
public function GetA() {
return $this->vars;
}
}
public SubBase extends Base {
public function __construct() {
$this->vars = array();
}
}
但是,我错过了一些东西...... 在角色中,我的 SubBase 是我的网站的子页面,Base 是 printHTML 类。我试图用我的 Base 类为我的网站设置一个标题,他在我的 SubBase 类 oO 中设置了 $this->vars 如果我错了,请告诉我,并让我知道如何写这个。 我只想写下来
<title> <?php echo Base::GetTitle(); ?> </title>
并展示出来。
i've one stupid question.
I keep trying to write my framework, but ... i miss something.
I have one base class Base.class.php, with some functions.
When, i write another class SubBase.class.php, who extends Base, i trying to set one variable , who must use in Base class, in one static function (maybe).
Something like that
class Base {
public $vars;
public function GetA() {
return $this->vars;
}
}
public SubBase extends Base {
public function __construct() {
$this->vars = array();
}
}
But, i missing something ...
In role, my SubBase is subpage from my web, Base is printHTML class. I trying to set a title to my web, with my Base class, who set $this->vars in my SubBase class o.O
Please, tell me if i'm wrong, and let me know how to write this.
I wonna just write
<title> <?php echo Base::GetTitle(); ?> </title>
and show in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您应该看一下静态属性和方法:http://php. net/manual/en/language.oop5.static.php
为了完成你想要的,你可以尝试以下操作:
注意,我使用了 SubBase::GetTitle();而不是 Base::GetTitle();如果您使用 Base::GetTitle(),您的输出将为空白,因为您正在使用基类中给定的值。在我的示例中,其:
但是,当您实例化 SubBase 类时,您提供一个“$newTitle”参数,然后该参数将覆盖空白值。
理想情况下,这应该可行。不过,我建议您更多地了解静态属性和方法的使用。
希望这有帮助。
Well, you should take a look at static properties and methods: http://php.net/manual/en/language.oop5.static.php
To accomplish what you want, you can try the following:
Note, that I used SubBase::GetTitle(); and not Base::GetTitle(); If you use Base::GetTitle(), you're output will be blank because you're using the value given at the Base Class. In my example, its:
However, when you instantiate your SubBase class, you provide a "$newTitle" parameter, which then overrides the blank value.
Ideally, this should work. Still, I recommend you learn more about the use of static properties and methods.
Hope this helps.