我很困惑,我有一个PHP班 和一个建筑师:
class Test {
public function __construct() {
return "some text";
}
}
然后,我即刻宣布一个物体:
$t = new Test();
我希望$t的内容是“一些文字”:
print_r($t);
但它是:
Test Object
(
)
如何从构建器获取返回值 < code> “ some text” code>?
我很困惑,我有一个PHP班 和一个建筑师:
class Test {
public function __construct() {
return "some text";
}
}
然后,我即刻宣布一个物体:
$t = new Test();
我希望$t的内容是“一些文字”:
print_r($t);
但它是:
Test Object
(
)
如何从构建器获取返回值 < code> “ some text” code>?
您无法从构建器获取 return
任何东西。 new
关键字将总是产生新对象, 与您从构建器获取的 return
无关。
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 ...
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 ...
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 ] ) {...
Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....
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 ...
For example is this correct: class C { private: C(); C(const & C other); } or you should rather provide definition(s): class C { private: C() {}; C(const & C ...
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 ...
Say you have this shell of a class: public class Number { private int value; public Number() : this(0) {} public Number(int initialValue) : this(initialValue, 0, 100) {} ...