使对象在 php 函数中可用,而不传递它们或将它们设为全局
这个要求只是为了开发者的简单和代码的美观。我正在构建一个模板系统,我真的希望在所有函数中都存在一个对象变量。这是一些代码:
Librarian.php:
$class = "slideshow";
$function = "basic";
$args = array(...);
$librarian = $this; // I WOULD LIKE THIS TO BE PRESENT IN CALLED FUNCTION
Slideshow.php
return call_user_func($class.'::'.$function, $args);
……
:
public static function basic($args) {
echo $librarian; // "Librarian Object"
}
谢谢! 马特·穆勒
This requirement is just for simplicity for developers and beautiful code. I'm building a template system and I really would just like an object variable to simply be there in all functions. Here's some code:
Librarian.php:
$class = "slideshow";
$function = "basic";
$args = array(...);
$librarian = $this; // I WOULD LIKE THIS TO BE PRESENT IN CALLED FUNCTION
...
return call_user_func($class.'::'.$function, $args);
...
Slideshow.php:
public static function basic($args) {
echo $librarian; // "Librarian Object"
}
Thanks!
Matt Mueller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以拥有一个使用的函数:
这样您就不必不断地将全局语句添加到每个函数中。
这就是你的意思吗?
You could have a function that you use:
That way you won't have to continually add the global statement to each function.
Is that what you meant?
我想您可以使用单例,但这在某种程度上属于
global
类别。您也可以在类之外使用单例:
您想要的东西是不可能的,至少我没有看到任何简单或优雅的方法来做到这一点。
I guess you could use a singleton but that would somewhat fall into the
global
category.You can also have the singleton outside the class:
What you want isn't possible, at least I don't see any simple or elegant way to do it.