php - 使用 PHP 中的扩展方法更改输出
问题描述
是否有可能在 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);
}
}
推荐阅读
- c# - MSIX Web 安装程序不工作 - 解析应用程序包时出错
- python - 基于其他两列创建一列
- android - 如何将自定义 SharedPreferences 名称与 AndroidX 首选项库一起使用?
- google-maps-api-3 - 大屏幕上的 Google Maps API 故障
- python - 如何将列添加到数据框使其成为第一列?
- python - 浏览文件并将路径存储在函数中
- python - 我可以在虚拟环境中增加 Python 使用的内存量吗
- jquery-select2 - 带有 select2 的浮动标签
- mvvm - 未调用 Kendo Observable 远程数据源更新
- java - 用公共构造函数Java声明一个私有静态嵌套类?