是否可以在 PHP 中执行类似的操作?
Image::myMethod(); // returns array
Image::myMethod()->toSring(); // Returns a string built inside the method toString
所以,换句话说,PHP 有没有办法检测该方法是否被另一种方法扩展,如果是,则返回 self/this
?
最佳答案
编码我在评论中的意思,这将在逻辑上为您提供相同的行为,尽管实现细节不同:
class Artifact extends ArrayObject {
public function toString(): string {
// custom stuff, use ArrayObject api to access underlying data
}
}
class Image {
public function myMethod(): Artifact {
// generate your array data, then:
return new Artifact($data);
}
}
关于php - 使用 PHP 中的扩展方法更改输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51949648/