处理对象数组和智能感知 [英] Handling array of object and intellisense

查看:62
本文介绍了处理对象数组和智能感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多时候我将自定义对象数组传递给函数进行处理.在我的函数中,在访问内部属性/方法之前,我会检查以确保它是正确的类.

I am finding a lot of the time I pass an array of custom objects to a function for processing. In my function I check to make sure it is the right class before accessing internal properties/methods.

因为我使用的是一个数组,而且据我所知 PHP 没有任何类型化的泛型列表,所以我无法使用类型提示,因此我的 IDE 中的智能感知不起作用,代码检查会抛出警告.

Because I am taking an array and as far as I am aware PHP doesn't have any typed generic list I cannot use type hinting so intellisense in my IDE doesn't work and code inspection throws warnings.

我看到了一篇旧帖子,其中提出了在开发过程中加入这行代码以使智能感知工作的想法:

I came across an old post that gave the idea of throwing in this line of code in order to get intellisense working while developing:

if (false) $myObj = new MyObject();

所以我最终得到了一个看起来像这样的函数:

So I end up with a function that looks like:

function ProcessObjectArray(array $arrayOfObject)
{
    foreach ($arrayOfObject as $key => $myObj) {
        if (get_class($myObj) == 'MyNamespace\MyObject') {
            if (false) $myObj = new MyObject();
            // I can now access intellisense for MyObject in here

        } else {
            trigger_error('is not right object');
        }
    }
}

这似乎有点奇怪,所以我想知道这是否表明我没有以最佳方式处理对象数组,或者是否有更好的方法让我的智能感知工作.我查看了 arrayobject 接口,看看我是否可以创建一个实现 arrayobject 的类来保存类型列表.虽然将所有验证放在其构造函数中或附加类似函数很不错,但我无法让它与智能感知一起工作,因为内部容器仍然只是一个标准数组.同样使用 get_class 似乎不太好,因为如果类名或命名空间被重命名,那么 IDE 重构功能不会选择这个引用(好吧,PHPStorm 至少没有,它是我使用).

It seems like a bit of a weird hack so I am wondering if this is a sign that I am not handling arrays of objects in the best way or if there is a better way to get my intellisense working. I had a look at the arrayobject interface to see if I could create a class implementing arrayobject that would hold a typed list. While it makes it nice to put all validation inside its constructor or append like functions I couldn't get it working with intellisense as the internal container is still just a standard array. Also using get_class doesn't seem good as if a class name or namespace is renamed then the IDE refactoring features do not pick this reference up (well, PHPStorm doesn't at least and it is one I am using).

我当然不需要智能感知,但是这个 hack 的怪异让我怀疑我是否有正确的 PHP 方法来处理 OOP,并认为我可能会遗漏一些东西.这是正确的做法还是我错过了什么?

I of course don't need intellisense but the weirdness of this hack made me wonder if I have the right approach to OOP in PHP and thought I might be missing something. Is this the right way of doing it or have I missed something?

推荐答案

使用 phpstorm 当您添加注释时,它会正确地为您完成代码.

with phpstorm when you add your annotations it will correctly do the code completion for you.

/**
 * @param MyNamespace\MyObject[] $arrayOfObject
 */
function ProcessObjectArray(array $arrayOfObject)
{
    foreach ($arrayOfObject as $key => $myObj) {
        if (get_class($myObj) == 'MyNamespace\MyObject') {
            if (false) $myObj = new MyObject();
            // I can now access intellisense for MyObject in here

        } else {
            trigger_error('is not right object');
        }
    }
}

然而,intellisense 不会阻止您传入不正确的对象,它只会在编码时帮助您防止传入不正确的对象.

However intellisense won't stop you from passing in incorrect objects, it will only help you while coding to prevent passing in the incorrect objects.

也许您可能希望考虑使用集合而不是通用数组.

Perhaps you may wish to consider using collections instead of using generic arrays.

有很多例子,但这里有一个简单的例子,你可以扩展

There are lots of examples out there, however here is a simple one which you can expand upon

class Collection 
{
    /** @var array */
    private $items = [];

    /**
     * @param MyNamespace\MyObject $obj
     * @return $this
     */
    public function addItem(MyNamespace\MyObject $obj) {
        $this->items[] = $obj;
        return $this;
    }

    /**
     * @param $index
     * @return $this
     */
    public function deleteItem($index) {
        unset($this->items[$index]);
        return $this;
    }

    /**
     * @param $index
     * @return MyNamespace\MyObject|null
     */
    public function getItem($index) {
        return array_key_exists($index, $this->items) ? $this->items[$index] : null;
    }

    /**
     * @return MyNamespace\MyObject[]
     */
    public function getItems() {
        return $this->items;
    }

    /**
     * @return int
     */
    public function count() {
        return sizeOf($this->items);
    }
}

这是一个如何运作的例子:

here is an example how it all works:

$collection = new \Collection;

$obj = new MyNamespace\MyObject;

$collection->addItem($obj);

foreach($collection->getItems() as $item) {
    // this will already know that $item is MyNamespace\MyObject because of the annotation on the getItems() method
    $item->doSomething();
}

这篇关于处理对象数组和智能感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆