php 在php中没有对象实例化的情况下调用类方法(带构造函数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3098881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling class method (with constructors) without object instantiation in php
提问by myk00
Ive looked and tried but I can't find an answer.
我看过并尝试过,但我找不到答案。
In PHP, is it possible to call a class' member function (when that class requires a constructor to receive parameters) without instantiating it as an object?
在 PHP 中,是否可以调用类的成员函数(当该类需要构造函数来接收参数时)而不将其实例化为对象?
A code example (which gives errors):
代码示例(给出错误):
<?php
class Test {
private $end="";
function __construct($value) {
$this->end=$value;
}
public function alert($value) {
echo $value." ".$this->end;
}
}
//this works:
$example=new Test("world");
$example->alert("hello");
//this does not work:
echo Test("world")::alert("hello");
?>
回答by Felipe Cardoso Martins
Unfortunately PHP doesn't have support to do this, but you are a creative and look guy :D
不幸的是 PHP 不支持这样做,但你是一个有创意和外观的人 :D
You can use an "factory", sample:
您可以使用“工厂”,示例:
<?php
class Foo
{
private $__aaa = null;
public function __construct($aaa)
{
$this->__aaa = $aaa;
}
public static function factory($aaa)
{
return new Foo($aaa);
}
public function doX()
{
return $this->__aaa * 2;
}
}
Foo::factory(10)->doX(); // outputs 20
回答by Andrew U
Just do this (in PHP >= 5.4):
只需执行此操作(在 PHP >= 5.4 中):
$t = (new Test("Hello"))->foo("world");
回答by CodeCavalier
I, too, was looking for a one-liner to accomplish this as part of a single expression for converting dates from one format to another. I like doing this in a single line of code because it is a single logical operation. So, this is a little cryptic, but it lets you instantiate and use a date object within a single line:
我也一直在寻找一个单行来实现这一点,作为将日期从一种格式转换为另一种格式的单个表达式的一部分。我喜欢用一行代码来做这件事,因为它是一个单一的逻辑操作。所以,这有点神秘,但它允许您在一行中实例化和使用日期对象:
$newDateString = ($d = new DateTime('2011-08-30') ? $d->format('F d, Y') : '');
Another way to one-line the conversion of date strings from one format to another is to use a helper function to manage the OO parts of the code:
另一种将日期字符串从一种格式转换为另一种格式的方法是使用辅助函数来管理代码的 OO 部分:
function convertDate($oldDateString,$newDateFormatString) {
$d = new DateTime($oldDateString);
return $d->format($newDateFormatString);
}
$myNewDate = convertDate($myOldDate,'F d, Y');
I think the object oriented approach is cool and necessary, but it can sometimes be tedious, requiring too many steps to accomplish simple operations.
我认为面向对象的方法很酷而且很有必要,但它有时会很乏味,需要太多的步骤来完成简单的操作。
回答by cletus
You can't call an instance-level method without an instance. Your syntax:
你不能在没有实例的情况下调用实例级方法。你的语法:
echo Test("world")::alert("hello");
echo Test("world")::alert("hello");
doesn't make a lot of sense. Either you're creating an inline instance and discarding it immediately or the alert()method has no implicit thisinstance.
没有多大意义。要么您正在创建一个内联实例并立即丢弃它,要么该alert()方法没有隐式this实例。
Assuming:
假设:
class Test {
public function __construct($message) {
$this->message = $message;
}
public function foo($message) {
echo "$this->message $message";
}
}
you can do:
你可以做:
$t = new Test("Hello");
$t->foo("world");
but PHP syntax doesn't allow:
但 PHP 语法不允许:
new Test("Hello")->foo("world");
which would otherwise be the equivalent. There are a few examples of this in PHP (eg using array indexing on a function return). That's just the way it is.
否则将是等效的。PHP 中有几个这样的例子(例如,在函数返回时使用数组索引)。就是那样子。

