public function defaultValue() { if ($this->property->isDefault()) { $defaultProperties = $this->property->getDeclaringClass()->getDefaultProperties(); return $defaultProperties[$this->property->getName()]; } return null; }
/** * Returns whether the property is a default property defined in the class. * * A default property is defined in the class definition. * A non-default property is an instance specific state. * @return bool */ public function isDefault() { if ($this->reflectionSource instanceof ReflectionProperty) { return $this->reflectionSource->isDefault(); } else { return parent::isDefault(); } }
function reflectProperty($class, $property) { $propInfo = new ReflectionProperty($class, $property); echo "**********************************\n"; echo "Reflecting on property {$class}::{$property}\n\n"; echo "isDefault():\n"; var_dump($propInfo->isDefault()); echo "\n**********************************\n"; }
function reflectProperty($class, $property) { $propInfo = new ReflectionProperty($class, $property); echo "**********************************\n"; echo "Reflecting on property {$class}::{$property}\n\n"; echo "isDefault():\n"; var_dump($propInfo->isDefault()); echo "getModifiers():\n"; var_dump($propInfo->getModifiers()); echo "getDeclaringClass():\n"; var_dump($propInfo->getDeclaringClass()); echo "getDocComment():\n"; var_dump($propInfo->getDocComment()); echo "\n**********************************\n"; }
public function scan() { require_once $this->path; $ns = ""; $_ns = ""; $ns_bracket = false; $aliases = []; $tokens = new Tokenizer(FS::get($this->path)); while ($tokens->valid()) { if ($tokens->is(T_NAMESPACE)) { $ns = ""; $_ns = ""; $tokens->next(); if ($tokens->is(T_STRING)) { $ns = $this->_parseName($tokens); if ($tokens->is('{')) { $tokens->skip(); $ns_bracket = true; } else { $tokens->skipIf(';'); } $_ns = $ns . '\\'; } elseif ($tokens->is('{')) { $ns_bracket = true; $tokens->next(); } } elseif ($tokens->is(T_USE)) { do { $tokens->next(); $name = $this->_parseName($tokens); if ($tokens->is(T_AS)) { $aliases[$tokens->next()->get(T_STRING)] = $name; $tokens->next(); } else { if (strpos($name, '\\') === false) { $aliases[$name] = $name; } else { $aliases[ltrim('\\', strrchr($name, '\\'))] = $name; } } } while ($tokens->is(',')); $tokens->need(';')->next(); } elseif ($tokens->is(T_CONST)) { $name = $tokens->next()->get(T_STRING); $constant = new EntityConstant($_ns . $name); $constant->setValue(constant($_ns . $name)); $constant->setLine($this->line($tokens->getLine())); $this->constants[$_ns . $name] = $constant; $tokens->forwardTo(';')->next(); } elseif ($tokens->is(T_FUNCTION)) { $name = $tokens->next()->get(T_STRING); $function = new EntityFunction($_ns . $name); $function->setLine($this->line($tokens->getLine())); $function->setAliases($aliases); $this->parseCallable($function, new \ReflectionFunction($function->name)); $function->setBody($tokens->forwardTo('{')->getScope()); $tokens->next(); $this->functions[$function->name] = $function; } elseif ($tokens->is(T_FINAL, T_ABSTRACT, T_INTERFACE, T_TRAIT, T_CLASS)) { $tokens->forwardTo(T_STRING); $name = $tokens->current(); $class = new EntityClass($_ns . $name); $ref = new \ReflectionClass($class->name); $doc = $ref->getDocComment(); // if($name == "NamesInterface") { // drop($ref); // } if ($ref->isInterface()) { $class->addFlag(Flags::IS_INTERFACE); } elseif ($ref->isTrait()) { $class->addFlag(Flags::IS_TRAIT); } else { $class->addFlag(Flags::IS_CLASS); } if ($ref->isAbstract()) { $class->addFlag(Flags::IS_ABSTRACT); } elseif ($ref->isFinal()) { $class->addFlag(Flags::IS_FINAL); } if ($doc) { $info = ToolKit::parseDoc($doc); $class->setDescription($info['desc']); $class->addOptions($info['options']); } $class->setAliases($aliases); $class->setLine($this->line($tokens->getLine())); $tokens->next(); if ($tokens->is(T_EXTENDS)) { // process 'extends' keyword do { $tokens->next(); $root = $tokens->is(T_NS_SEPARATOR); $parent = $this->_parseName($tokens); if ($root) { // extends from root namespace $class->setParent($parent, $class->isInterface()); } elseif (isset($aliases[$parent])) { $class->setParent($aliases[$parent], $class->isInterface()); } else { $class->setParent($_ns . $parent, $class->isInterface()); } } while ($tokens->is(',')); } if ($tokens->is(T_IMPLEMENTS)) { // process 'implements' keyword do { $tokens->next(); $root = $tokens->is(T_NS_SEPARATOR); $parent = $this->_parseName($tokens); if ($root) { // extends from root namespace $class->addInterface($parent); } elseif (isset($aliases[$parent])) { $class->addInterface($aliases[$parent]); } else { $class->addInterface($_ns . $parent); } } while ($tokens->is(',')); } $tokens->forwardTo('{')->next(); while ($tokens->forwardTo(T_CONST, T_FUNCTION, '{', '}', T_VARIABLE) && $tokens->valid()) { switch ($tokens->key()) { case T_CONST: $constant = new EntityConstant($class->name . '::' . $tokens->next()->get(T_STRING)); $constant->setValue(constant($constant->name)); $constant->setLine(new Line($this, $tokens->getLine())); $class->addConstant($constant); break; case T_VARIABLE: $property = new EntityProperty(ltrim($tokens->getAndNext(), '$')); $ref = new \ReflectionProperty($class->name, $property->name); $doc = $ref->getDocComment(); if ($doc) { $property->setDescription(ToolKit::parseDoc($doc)['desc']); } if ($ref->isPrivate()) { $property->addFlag(Flags::IS_PRIVATE); } elseif ($ref->isProtected()) { $property->addFlag(Flags::IS_PROTECTED); } else { $property->addFlag(Flags::IS_PUBLIC); } if ($ref->isStatic()) { $property->addFlag(Flags::IS_STATIC); } if ($ref->isDefault()) { $property->setValue($ref->getDeclaringClass()->getDefaultProperties()[$property->name]); } $class->addProperty($property); break; case T_FUNCTION: $method = new EntityMethod($name . '::' . $tokens->next()->get(T_STRING)); $method->setLine($this->line($tokens->getLine())); $this->parseCallable($method, $ref = new \ReflectionMethod($class->name, $method->short)); if ($ref->isPrivate()) { $method->addFlag(Flags::IS_PRIVATE); } elseif ($ref->isProtected()) { $method->addFlag(Flags::IS_PROTECTED); } else { $method->addFlag(Flags::IS_PUBLIC); } if ($ref->isStatic()) { $method->addFlag(Flags::IS_STATIC); } if ($ref->isAbstract()) { $method->addFlag(Flags::IS_ABSTRACT); $method->addFlag(Flags::IS_ABSTRACT_IMPLICIT); } elseif ($ref->isFinal()) { $method->addFlag(Flags::IS_FINAL); } if (isset($method->options['deprecated'])) { $method->addFlag(Flags::IS_DEPRECATED); } $tokens->forwardTo(')')->next(); if ($tokens->is('{')) { $method_body = $tokens->getScope(); $method->setBody($method_body); } $tokens->next(); $class->addMethod($method); break; case '{': // use traits scope $tokens->forwardTo('}')->next(); break; case '}': // end of class $tokens->next(); $this->classes[$class->name] = $class; break 2; } } } elseif ($tokens->is('}') && $ns_bracket) { $tokens->next(); $ns_bracket = false; } else { drop($tokens->curr); if ($tokens->valid()) { throw new UnexpectedTokenException($tokens); } break; } } }
<?php class A { public $defprop; } $a = new A(); $a->myprop = null; $ro = new ReflectionObject($a); $props = $ro->getProperties(); $prop1 = $props[0]; var_dump($prop1->isDefault()); $prop2 = $props[1]; var_dump($prop2->isDefault()); var_dump($ro->getProperty('defprop')->isDefault()); var_dump($ro->getProperty('myprop')->isDefault()); $prop1 = new ReflectionProperty($a, 'defprop'); $prop2 = new ReflectionProperty($a, 'myprop'); var_dump($prop1->isDefault()); var_dump($prop2->isDefault()); ?> ==DONE==
public function testSetUndefinedProperty() { $ins = $this->Basic::newWithoutConstructor(); $ins->undefined = 'newValue'; $this->assertEquals('newValue', $ins->undefined); (function ($tester) { $rp = new \ReflectionProperty($this->ins, 'undefined'); $tester->assertFalse($rp->isDefault()); })->call($ins, $this); }
<?php class C { public static $p; } var_dump(new ReflectionProperty()); var_dump(new ReflectionProperty('C::p')); var_dump(new ReflectionProperty('C', 'p', 'x')); $rp = new ReflectionProperty('C', 'p'); var_dump($rp->getName(1)); var_dump($rp->isPrivate(1)); var_dump($rp->isProtected(1)); var_dump($rp->isPublic(1)); var_dump($rp->isStatic(1)); var_dump($rp->getModifiers(1)); var_dump($rp->isDefault(1));
<pre> <?php class String { public $length = 5; } // Создание экземпляра класса ReflectionProperty $prop = new ReflectionProperty('String', 'length'); // Вывод основной информации о свойстве класса printf("===> %s%s%s%s свойство '%s' (которое было %s)\n" . " имеет модификаторы %s\n", $prop->isPublic() ? ' public' : '', $prop->isPrivate() ? ' private' : '', $prop->isProtected() ? ' protected' : '', $prop->isStatic() ? ' static' : '', $prop->getName(), $prop->isDefault() ? 'объявлено во время компиляции' : 'создано во время выполнения', var_export(Reflection::getModifierNames($prop->getModifiers()), 1)); exit; // Создание экземпляра String $obj = new String(); // Получение текущего значения printf("---> Значение: "); var_dump($prop->getValue($obj)); // Изменение значения $prop->setValue($obj, 10); printf("---> Установка значения 10, новое значение равно: "); var_dump($prop->getValue($obj)); // Дамп объекта var_dump($obj); ?> </pre>
<?php class String { public $length = 5; function accessLengthProp(string $obj, $length) { $reflection = new ReflectionClass($obj); $property = $reflection->getProperty($length); $property->setAccessible(true); return $property->getValue($obj); } } $prop = new ReflectionProperty('String', 'length'); printf("<br/>===> The%s%s%s%s property '%s' (which was %s) having the modifiers %s<br/>", $prop->isPublic() ? ' public' : '', $prop->isPrivate() ? ' private' : '', $prop->isProtected() ? ' protected' : '', $prop->isStatic() ? ' static' : '', $prop->getName(), $prop->isDefault() ? 'declared at compile-time' : 'created at run-time', var_export(Reflection::getModifierNames($prop->getModifiers()), 1)); $obj = new String(); echo "<br/>===> The length property of the object is " . $obj->accessLengthProp($obj, 'length') . "<br/>"; printf("<br/>===> The value is: "); echo $prop->getValue($obj) . "<br/>"; $prop->setValue($obj, 10); printf("<br/>===> The setting value to 10, new value is: "); echo " " . $prop->getValue($obj); /* output ===> The public property 'length' (which was declared at compile-time) having the modifiers array ( 0 => 'public', ) ===> The length property of the object is 5 ===> The value is: 5 ===> The setting value to 10, new value is: 10
/** * Clone parameter options using ReflectionProperty. * * @param \ReflectionProperty $property */ public function cloneSchema(\ReflectionProperty $property) { $this->setDefault(false); $this->setComment($property->getDocComment()); $this->static = $property->isStatic(); if ($property->isPrivate()) { $this->setAccess(self::ACCESS_PRIVATE); } elseif ($property->isProtected()) { $this->setAccess(self::ACCESS_PROTECTED); } if (!$property->isDefault()) { return; } $parentDefaults = $property->getDeclaringClass()->getDefaultProperties(); foreach ($parentDefaults as $name => $defaultValue) { if ($name == $property->getName()) { $this->setDefault(true, $defaultValue); break; } } }