开发者

Operator overloading equivalent in C++ for PHP, echo/print class variable for default output

I don't know how to really ask this because I am fairly new to programming in comparison to many of you. What I am looking for is a default printing or echoing of a class. I'll give you the c++ equivalent.

ClassName varClass(param);

cout << "Default print: " << varClass << endl;

As you can see in that brief example, instead of having to call varClass.customPrintFunction(), I only had to use the variable name.

What I need is the php equivalent to that. What in php would allow me to do this:

$address = new Address(param);

echo "Default p开发者_运维问答rint: " . $address . "<br />";

Instead of: echo "Default print: " . $address->customPrintFunction() . "<br />";

I hope I was clear enough. If there isn't an equivalent, if you could give me what would be my best option instead. Thanks in advanced.


You can define a __toString method that defines the behavior of the object in case it is cast to a string.

public function __toString() { return $this->customPrintFunction(); }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜