*对不起,我现在正在学英语,我的英语还不太好。请理解我的处境。
据我所知,静态需要使用类::函数(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
发布于 2015-02-01 16:00:08
在这种情况下,他们使用的是self,它不需要静态,您可以将self::与$this->进行比较,只是self::也适用于静态函数。
也许手册能帮你
发布于 2015-02-01 15:51:50
不确定这是否是您想要做的,但是您可以在php public static function methodName()中声明“静态”,然后用Class::Method()调用函数,您也可以查看这获取更多关于静态的数据。
编辑:
这是来自php.net的
当从对象上下文中调用方法时,伪变量$this是可用的。$this是对调用对象的引用(通常是方法所属的对象,但如果方法是从次要对象的上下文静态调用的,则可能是另一个对象)。
因此,基本上您可以这样做(调用类方法静态方式)。
https://stackoverflow.com/questions/28264450
复制相似问题