首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Class::Function(params)而不使用静态

如何使用Class::Function(params)而不使用静态
EN

Stack Overflow用户
提问于 2015-02-01 15:38:16
回答 3查看 70关注 0票数 0

*对不起,我现在正在学英语,我的英语还不太好。请理解我的处境。

据我所知,静态需要使用类::函数(Params);

就像这张。

代码语言:javascript
运行
复制
class Foo {
    static function Bar($msg){
        echo $msg;
    }
}

XE中有一个文件(在韩国开发的is CMS )。

(XE官方网站:http://www.xpressengine.com/?l=en)

当然,这是一个真实文件的摘要。

代码语言:javascript
运行
复制
<?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,并包含在其他文件中。

代码语言:javascript
运行
复制
<?php
    require(_XE_PATH_ . 'classes/context/Context.class.php');
    Context::addBodyHeader("Some Codes");
?>
EN

Stack Overflow用户

回答已采纳

发布于 2015-02-01 16:41:03

在这个注释中,它可以使用Class::Function()和我在XE中使用过。但它不能说明它们是如何制造的。我怎么才能像这样呢?::被称为范围分解算子

它们的规定如下:

代码语言:javascript
运行
复制
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

票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28264450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档