在 PHP 中返回静态类
我正在开发一个后端项目。我需要返回一个带有另一个静态对象的静态对象:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你不需要那个。
另外
,这是错误且具有误导性的
view()->script_link
因为 script_link 是静态的附录
如果您的问题是您的类名长度,我建议您为此创建简单的包装器。
这样你只需要
createLink();
You don't need that.
Use
Also this is wrong and misleading
view()->script_link
because script_link is staticAddendum
If you your problem is your class name length I suggest you to create simple wrapper for this.
this way you just need to
createLink();
在 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 ofreturn View::self;
).Manual: http://php.net/manual/en/language.oop5.basic.php#example-159
在 php 5.2 中使用
ReflectionClass
in php 5.2 use
ReflectionClass
我认为你的调用语法是错误的。因为它是静态的,所以您尝试要做的事情看起来像这样:
除非这会给您带来语法错误。此外,由于它是静态的,因此您不需要返回任何内容。您应该对其进行两次单独的调用:
说“我需要返回一个静态对象”是没有意义的。如果定义了类,则静态对象就存在并且可以访问。
I think your syntax on the call is wrong. Since it is static, what you are trying to do would look something like this:
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:
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.
您只需要一个变量来保存该类,因为直接调用是无效语法
样本:
You just need a variable to hold the class, as a direct call is invalid syntax
Sample:
您是否正在寻找后期静态绑定的功能? 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