首页 > 解决方案 > 不在 PHP 的对象上下文中使用 $this 时出错

问题描述

在尝试使用 Trait 从传入的字段构建类属性和字段时,我遇到了以下错误“未捕获的错误:不在对象上下文中使用 $this”。

我的方法:

public function createNewPet(array $petData): bool {
  return self::save($petData);
}

我的方法是调用save()方法:

/**
 * @param array|null $fields
 * @return bool|null
 * @throws \ReflectionException
*/
public function save(?array $fields = null): ?bool
{
        $values = [];
        if (isset($fields)) {
            foreach (Helper::getNonStaticProperties(static::class, false) as $prop) {
                if (!property_exists(static::class, $prop)) {
                    continue;
                }
                $values[$prop] = $this->{$prop};
            }
        } else {
            if (!is_array($fields)) {
                $fields = [$fields];
            }
            foreach ($fields as $field) {
                if (property_exists(static::class, $field)) {
                    $values[$field] = $this->{$field};
                }
            }
        }
}

您可以看到此方法引用了以下辅助方法getNonStaticProperties()

/**
     * @param string $object
     * @param bool $includeParentProperties
     * @param bool $includeTraitProperties
     * @return array
     * @throws \ReflectionException
     */
    public static function getNonStaticProperties(
        string $object,
        bool $includeParentProperties = true,
        bool $includeTraitProperties = false
    ) :array
    {
        if (!class_exists($object)) {
            return []; //If $object is not a valid class, return empty array
        }
        //Create a reflection of the passed object
        $reflection = new \ReflectionClass($object);
        $static = $reflection->getStaticProperties(); //Get the reflections static properties
        $allArr = $reflection->getProperties(); //Get the reflections properties

        $all = null;

        $static = array_keys($static);
        foreach ($allArr as $prop) {
            $all[] = trim($prop->name);
        }
        if (!is_array($all)) {
            $all = [$all];
        }
        if (!is_array($static) || empty($static)) { //If $static is empty, simply return $all
            $result = $all;
        } else { // Return the list of variables that are present in $all but not present in $static
            $result =  array_diff($all, $static);
        }
        if (!$includeParentProperties && $parent = get_parent_class($object)) {
            $parentProps = self::getNonStaticProperties($parent, true);
            $result = array_diff($result, $parentProps);
        }
        if ($includeTraitProperties && !empty($traits = $reflection->getTraits())) {
            $traitProps = [];
            foreach ($traits as $trait) {
                $traitProperties = $trait->getProperties();
                foreach ($traitProperties as $prop) {
                    $traitProps[] = trim($prop->getName());
                }
            }
            $result = array_diff($result, $traitProps);
        }
        return $result;
    }

更新:将方法(显示在评论中)添加到问题中。

标签: phpoop

解决方案


public function createNewPet(array $petData): bool {
  return self::save($petData);
}

您正在静态调用save非静态方法。替换self::save$this->save应该修复它。


推荐阅读