gpt4 book ai didi

php - 在 PHP 中指定类的对象类型的方法

转载 作者:可可西里 更新时间:2023-10-31 22:10:27 25 4
gpt4 key购买 nike

有没有办法在 PHP 中指定对象的属性类型?例如,我有类似的东西:

class foo{
public bar $megacool;//this is a 'bar' object
public bar2 $megasupercool;//this is a 'bar2' object
}


class bar{...}
class bar2{...}

如果没有,您知道有一天在 PHP 的 future 版本之一中是否有可能吗?

最佳答案

除了已经提到的 TypeHinting 之外,您还可以记录该属性,例如

class FileFinder
{
/**
* The Query to run against the FileSystem
* @var \FileFinder\FileQuery;
*/
protected $_query;

/**
* Contains the result of the FileQuery
* @var Array
*/
protected $_result;

// ... more code

@var annotation会帮助一些 IDE 提供代码帮助。

关于php - 在 PHP 中指定类的对象类型的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3165089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com