本文整理汇总了PHP中ReflectionParameter::getDefaultValue方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionParameter::getDefaultValue方法的具体用法?PHP ReflectionParameter::getDefaultValue怎么用?PHP ReflectionParameter::getDefaultValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionParameter
的用法示例。
在下文中一共展示了ReflectionParameter::getDefaultValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getDeclaration
public function getDeclaration()
{
if ($this->reflector->isArray()) {
$code = 'array ';
} else {
$class = $this->reflector->getClass();
if ($class !== null) {
$code = $class->name . ' ';
} else {
$code = '';
}
}
$code .= '$' . $this->reflector->name;
if ($this->reflector->isOptional()) {
$default = $this->reflector->getDefaultValue();
if (is_null($default)) {
$default = 'null';
} elseif (is_bool($default)) {
$default = $default ? 'true' : 'false';
} elseif (is_string($default)) {
$default = "'" . $default . "'";
} elseif (is_numeric($default)) {
$default = strval($default);
} elseif (is_array($default)) {
$default = 'array()';
} else {
echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . PHP_EOL;
var_dump($default);
$default = 'null';
}
$code .= ' = ' . $default;
}
return $code;
}
示例2: getType
public function getType() : Type
{
if ($this->type === null) {
$phpDocType = $this->phpDocType;
if ($phpDocType !== null && $this->reflection->isDefaultValueAvailable() && $this->reflection->getDefaultValue() === null) {
$phpDocType = $phpDocType->makeNullable();
}
$this->type = TypehintHelper::decideTypeFromReflection($this->reflection->getType(), $phpDocType, $this->reflection->getDeclaringClass() !== null ? $this->reflection->getDeclaringClass()->getName() : null, $this->reflection->isVariadic());
}
return $this->type;
}
示例3: exportCode
public function exportCode()
{
$default_value = null;
if ($this->_parameter->isDefaultValueAvailable()) {
$default_value = $this->_parameter->getDefaultValue();
if (is_scalar($default_value) && !is_numeric($default_value)) {
$default_value = "'{$default_value}'";
}
} elseif ($this->_parameter->isOptional()) {
$default_value = 'NULL';
}
return sprintf('%s%s$%s%s', $this->_parameter->getClass() ? "{$this->_parameter->getClass()->getName()} " : '', $this->_parameter->isPassedByReference() ? '&' : '', $this->_parameter->getName(), $default_value ? " = {$default_value}" : '');
}
示例4: test
function test($param)
{
$r = new ReflectionParameter('params', $param);
var_dump($r->getDefaultValue());
var_dump($r->getDefaultValueText());
var_dump($r->getDefaultValueConstantName());
}
示例5: getParameterValue
/**
* {@inheritdoc}
*/
public function getParameterValue(\ReflectionParameter $parameter)
{
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw UnresolvedValueException::unresolvedParameter($parameter);
}
示例6: __construct
public function __construct(\ReflectionParameter $parameter)
{
$this->name = $parameter->getName();
$this->position = $parameter->getPosition();
$this->has_default = $parameter->isDefaultValueAvailable();
$this->default_value = $this->getHasDefault() ? $parameter->getDefaultValue() : null;
}
示例7: fromReflection
/**
* Creates a PHP parameter from reflection
*
* @param \ReflectionParameter $ref
* @return PhpParameter
*/
public static function fromReflection(\ReflectionParameter $ref)
{
$parameter = new static();
$parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
if ($ref->isDefaultValueAvailable()) {
$parameter->setDefaultValue($ref->getDefaultValue());
}
// find type and description in docblock
$docblock = new Docblock($ref->getDeclaringFunction());
$params = $docblock->getTags('param');
$tag = $params->find($ref->name, function (ParamTag $t, $name) {
return $t->getVariable() == '$' . $name;
});
if ($tag !== null) {
$parameter->setType($tag->getType(), $tag->getDescription());
}
// set type if not found in comment
if ($parameter->getType() === null) {
if ($ref->isArray()) {
$parameter->setType('array');
} elseif ($class = $ref->getClass()) {
$parameter->setType($class->getName());
} elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
$parameter->setType('callable');
}
}
return $parameter;
}
示例8: __construct
public function __construct(ReflectionParameter $param)
{
if (method_exists('ReflectionParameter', 'getType')) {
if ($type = $param->getType()) {
$this->type_hint = (string) $type;
}
} else {
if ($param->isArray()) {
$this->type_hint = 'array';
} else {
try {
if ($this->type_hint = $param->getClass()) {
$this->type_hint = $this->type_hint->name;
}
} catch (ReflectionException $e) {
preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches);
$this->type_hint = isset($matches[1]) ? $matches[1] : '';
}
}
}
$this->reference = $param->isPassedByReference();
$this->position = $param->getPosition();
$this->name = $param->getName();
if ($param->isDefaultValueAvailable()) {
$this->default = var_export($param->getDefaultValue(), true);
}
}
示例9: from
/**
* @return self
*/
public static function from(\ReflectionParameter $from)
{
$param = new static($from->getName());
$param->reference = $from->isPassedByReference();
if (PHP_VERSION_ID >= 70000) {
$param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
} elseif ($from->isArray()) {
$param->typeHint = 'array';
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
$param->typeHint = 'callable';
} else {
try {
$param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
$param->typeHint = $m[1];
} else {
throw $e;
}
}
}
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
$param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
return $param;
}
示例10: from
/**
* @return self
*/
public static function from(\ReflectionParameter $from)
{
$param = new static();
$param->name = $from->getName();
$param->reference = $from->isPassedByReference();
if ($from->isArray()) {
$param->typeHint = 'array';
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
$param->typeHint = 'callable';
} else {
try {
$param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
$param->typeHint = '\\' . $m[1];
} else {
throw $e;
}
}
}
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
$param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
$namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL;
$namespace = $namespace ? "\\{$namespace}\\" : '\\';
if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
$param->typeHint = substr($param->typeHint, strlen($namespace));
}
return $param;
}
示例11: getDefaultValue
/**
* Returns the default value of this parameter or throws an exception
* @return mixed
* @since PHP 5.0.3
*/
public function getDefaultValue()
{
if ($this->parameter != null) {
return $this->parameter->getDefaultValue();
} else {
return parent::getDefaultValue();
}
}
示例12: getDefaultValue
private function getDefaultValue(\ReflectionParameter $parameter)
{
$defaultValue = null;
if ($parameter->isDefaultValueAvailable()) {
$defaultValue = $parameter->getDefaultValue();
}
return $defaultValue;
}
示例13: resolveNonClass
/**
* @param ReflectionParameter $parameter
* @return mixed
* @throws Exception
*/
public function resolveNonClass($parameter)
{
// 有默认值则返回默认值
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw new Exception('I have no idea what to do here.');
}
示例14: _createFromReflection
/**
* @load
* @param ReflectionParameter $reflection
*/
private function _createFromReflection($reflection)
{
$this->_name = $reflection->getName();
if ($reflection->isDefaultValueAvailable()) {
$this->_default = true;
$this->_value = $reflection->getDefaultValue();
}
}
示例15: getParameterDependency
/**
* @param Horde_Injector $injector
* @param ReflectionParameter $method
*
* @return mixed
* @throws Horde_Injector_Exception
*/
public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
{
if ($parameter->getClass()) {
return $injector->getInstance($parameter->getClass()->getName());
} elseif ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
}