English 中文(简体)
PHP 从构造器获取返回值
原标题:PHP Get Returned Value From Constructor

我很困惑,我有一个PHP班 和一个建筑师:

class Test {
   public function __construct() {
      return "some text";
   }
}

然后,我即刻宣布一个物体:

$t = new Test();

我希望$t的内容是“一些文字”:

print_r($t);

但它是:

Test Object
(
)

如何从构建器获取返回值 < code> “ some text” ?

问题回答

您无法从构建器获取 return 任何东西。 new 关键字将总是产生新对象, 与您从构建器获取的 return 无关。





相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

热门标签