Code Monkey home page Code Monkey logo

Comments (3)

wi1dcard avatar wi1dcard commented on June 3, 2024 1

首先感谢关注!

PHP 允许静态方法被动态调用,通过 isset($this) 可以判断当前方法是否处在类实例的上下文中。因此如果 isset($this) 为 TRUE,则可以使用 $this,否则创建自身。印象中 Laravel 经常有这种语法糖。

您可以看一下单元测试,在几个常见 PHP 版本都是可以跑通过的。

https://coveralls.io/builds/21963969/source?filename=aop/AlipayRequestFactory.php#L95

from alipay-sdk-php.

xiaoshangmin avatar xiaoshangmin commented on June 3, 2024

测试了确实可以 类实例->静态方法 这样调用 涨知识
但是没有找到这方面的知识点 请问有相关文档分享下吗 感谢

from alipay-sdk-php.

wi1dcard avatar wi1dcard commented on June 3, 2024

这是由于 PHP 的面向对象特性决定的。

根据 官方文档 的描述:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.

使用 static 关键字并不是真正地将方法定义为「静态方法」,而是「makes them accessible without needing an instantiation of the class(使它们无需类实例也可被访问)」。你可测试以下代码:

class FooBar
{
  public static function foo() {}

  public function foo() {}
}

// Fatal error: Cannot redeclare FooBar::foo()

无论方法是否使用 static,它们都是方法,不能重名。

另外,文档中有明确写道:

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

静态方法可通过类实例访问,但静态属性不可以。

from alipay-sdk-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.