在 PHP 中返回静态类

发布于 2024-11-13 08:17:36 字数 567 浏览 12 评论 0原文

我正在开发一个后端项目。我需要返回一个带有另一个静态对象的静态对象:

Class this_is_a_very_long_class_name
{
    public static function call()
    {
        return self;
    }

    public static function script_link($link)
    {
        //doing stuff here...
    }
}

Class Main
{
    public static function view()
    {
        // trying to return View object
        return this_is_a_very_long_class_name::call();
    }
}

并且我尝试像这样使用它:

Main::view()::script_link('Some script');

那么我该如何实现呢?

PS:我不是在寻找其他解决方案。我正在寻找我所问的答案。

I am working on a backend project. I need to return a static object withing another static object:

Class this_is_a_very_long_class_name
{
    public static function call()
    {
        return self;
    }

    public static function script_link($link)
    {
        //doing stuff here...
    }
}

Class Main
{
    public static function view()
    {
        // trying to return View object
        return this_is_a_very_long_class_name::call();
    }
}

and I am trying to use it like this:

Main::view()::script_link('Some script');

So how can I accomplish that?

P.S.: I am not looking for another solution. I am looking for a answer what I asked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

樱娆 2024-11-20 08:17:36

你不需要那个。

另外

View::script_link();

,这是错误且具有误导性的 view()->script_link 因为 script_link 是静态的

附录

如果您的问题是您的类名长度,我建议您为此创建简单的包装器。

function createLink($string){
 return VERY_LONG_CLASS_NAME_HELLO_PHP_NAMESPACE::script_link($string);
}

这样你只需要 createLink();

You don't need that.

Use

View::script_link();

Also this is wrong and misleading view()->script_link because script_link is static

Addendum

If you your problem is your class name length I suggest you to create simple wrapper for this.

function createLink($string){
 return VERY_LONG_CLASS_NAME_HELLO_PHP_NAMESPACE::script_link($string);
}

this way you just need to createLink();

等风来 2024-11-20 08:17:36

在 php 5.3 中:return new View();(而不是 return View::self;)。
手册: http://php.net/manual/en/language .oop5.basic.php#example-159

in php 5.3: return new View(); (instead of return View::self;).
Manual: http://php.net/manual/en/language.oop5.basic.php#example-159

月亮是我掰弯的 2024-11-20 08:17:36

在 php 5.2 中使用 ReflectionClass

in php 5.2 use ReflectionClass

荆棘i 2024-11-20 08:17:36

我认为你的调用语法是错误的。因为它是静态的,所以您尝试要做的事情看起来像这样:

Main::view()::script_link('Some script');

除非这会给您带来语法错误。此外,由于它是静态的,因此您不需要返回任何内容。您应该对其进行两次单独的调用:

Main::view();
View::script_link("Some script");

说“我需要返回一个静态对象”是没有意义的。如果定义了类,则静态对象就存在并且可以访问。

I think your syntax on the call is wrong. Since it is static, what you are trying to do would look something like this:

Main::view()::script_link('Some script');

Except that would give you a syntax error. Also, since it is static, you don't need to return anything. You should make it two separate calls:

Main::view();
View::script_link("Some script");

It makes no sense to say "I need to return a static object". If the class is defined, then the static object is present and can be accessed.

枯寂 2024-11-20 08:17:36

您只需要一个变量来保存该类,因为直接调用是无效语法
样本:

Class Main
{
    public static function view($type)
    {
        // return some class
        switch ($type) {
            case "View 2": 
                return View2;
                break;
            default:
                return View;
        }
    }
}

$v = Main::view("normal view");
$v::script_link('test');

You just need a variable to hold the class, as a direct call is invalid syntax
Sample:

Class Main
{
    public static function view($type)
    {
        // return some class
        switch ($type) {
            case "View 2": 
                return View2;
                break;
            default:
                return View;
        }
    }
}

$v = Main::view("normal view");
$v::script_link('test');
ぽ尐不点ル 2024-11-20 08:17:36

您是否正在寻找后期静态绑定的功能? PHP 5.3 开始支持。请参阅此处: http://php.net/manual/en/ language.oop5.late-static-bindings.php

Are you looking for functionality as late static binding? Which is supported from PHP 5.3. See here: http://php.net/manual/en/language.oop5.late-static-bindings.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文