C++ 中的运算符重载等效项对于 PHP,默认输出的 echo/print 类变量
我不知道如何真正问这个问题,因为与你们中的许多人相比,我对编程相当陌生。我正在寻找的是类的默认打印或回显。我会给你 C++ 等价的。
ClassName varClass(param);
cout << "Default print: " << varClass << endl;
正如您在该简短示例中所看到的,我不必调用 varClass.customPrintFunction()
,而只需使用变量名称。
我需要的是与之相当的 php。 php 中的什么可以让我这样做:
$address = new Address(param);
echo "Default print: " . $address . "<br />";
而不是: echo "Default print: " 。 $地址->customPrintFunction() 。 “
”;
我希望我说得足够清楚。如果没有同等的,如果你能给我什么是我最好的选择。
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 print: " . $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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以定义
__toString< /code>
方法,定义对象在转换为字符串时的行为。
You can define a
__toString
method that defines the behavior of the object in case it is cast to a string.