0

While trying to build a class properties and fields from passed in fields using a Trait, I am running into the following error "Uncaught Error: Using $this when not in object context".

My method:

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

My method is calling the save() method:

/**
 * @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};
                }
            }
        }
}

You can see this method references the following helper method 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;
    }

Update: Added the method (shown in comment) to the question.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • From a brief read, I don't think the code you shown would result in that error. Can you show the code that calls the `save` method? – Koala Yeung Sep 30 '19 at 16:05
  • `public function createNewPet(array $petData): bool { return self::save($petData); }` calls this above logic – Zach Smith Sep 30 '19 at 16:07
  • You are calling `save`, a non-static method, statically. Replace `self::save` with `$this->save` should fix it. – Koala Yeung Sep 30 '19 at 16:12

1 Answers1

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

You are calling save, a non-static method, statically. Replace self::save with $this->save should fix it.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50