*对不起,我现在正在学英语,我的英语还不太好。请理解我的处境。
据我所知,静态需要使用类::函数(Params);
就像这张。
class Foo {
static function Bar($msg){
echo $msg;
}
}XE中有一个文件(在韩国开发的is CMS )。
(XE官方网站:http://www.xpressengine.com/?l=en)
当然,这是一个真实文件的摘要。
<?php
/**
* Manages Context such as request arguments/environment variables
* It has dual method structure, easy-to use methods which can be called as self::methodname(),and methods called with static object.
*/
class Context
{
/**
* codes after <body>
* @var string
*/
public $body_header = NULL;
/**
* returns static context object (Singleton). It's to use Context without declaration of an object
*
* @return object Instance
*/
function &getInstance()
{
static $theInstance = null;
if(!$theInstance)
{
$theInstance = new Context();
}
return $theInstance;
}
/**
* Add html code after <body>
*
* @param string $header Add html code after <body>
*/
function addBodyHeader($header)
{
is_a($this, 'Context') ? $self = $this : $self = self::getInstance();
$self->body_header .= "\n" . $header;
}
}这是该文件顶部的注释。
它具有双重的方法结构,易于使用的方法,可以作为self::methodname()调用,以及使用静态对象调用的方法。
在这个注释中,它可以使用Class::Function()和我在XE中使用过。
但它不能说明它们是如何制造的。我怎么才能像这样呢?
Edit1 :
该文件名为Context.class.php,并包含在其他文件中。
<?php
require(_XE_PATH_ . 'classes/context/Context.class.php');
Context::addBodyHeader("Some Codes");
?>发布于 2015-02-01 16:41:03
在这个注释中,它可以使用Class::Function()和我在XE中使用过。但它不能说明它们是如何制造的。我怎么才能像这样呢?
::被称为范围分解算子。
它们的规定如下:
class MyClass {
public static function saySomething() {
echo 'hello';
}
public function sayHello() {
echo 'hello';
}
public function helloSay() {
self::sayHello();
}
}MyClass::saySomething();
MyClass::sayHello();
MyClass::helloSay();
它们全部输出:hello
https://stackoverflow.com/questions/28264450
复制相似问题