本文整理汇总了PHP中ReflectionClass::hasMethod方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::hasMethod方法的具体用法?PHP ReflectionClass::hasMethod怎么用?PHP ReflectionClass::hasMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::hasMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: evaluateCallback
/**
* Evaluate the callback and create an object instance if required and possible.
*
* @param array|callable $callback The callback to invoke.
*
* @return array|callable
*/
protected static function evaluateCallback($callback)
{
if (is_array($callback) && count($callback) == 2 && is_string($callback[0]) && is_string($callback[1])) {
$class = new \ReflectionClass($callback[0]);
// Ff the method is static, do not create an instance.
if ($class->hasMethod($callback[1]) && $class->getMethod($callback[1])->isStatic()) {
return $callback;
}
// Fetch singleton instance.
if ($class->hasMethod('getInstance')) {
$getInstanceMethod = $class->getMethod('getInstance');
if ($getInstanceMethod->isStatic()) {
$callback[0] = $getInstanceMethod->invoke(null);
return $callback;
}
}
// Create a new instance.
$constructor = $class->getConstructor();
if (!$constructor || $constructor->isPublic()) {
$callback[0] = $class->newInstance();
} else {
$callback[0] = $class->newInstanceWithoutConstructor();
$constructor->setAccessible(true);
$constructor->invoke($callback[0]);
}
}
return $callback;
}
示例2: mapBy
/**
* Map collection by key. For objects, return the property, use
* get method or is method, if avaliable.
*
* @param array $input Array of arrays or objects.
* @param string $key Key to map by.
* @throws \InvalidArgumentException If array item is not an array or object.
* @throws \LogicException If array item could not be mapped by given key.
* @return array Mapped array.
*/
public function mapBy(array $input, $key)
{
return array_map(function ($item) use($key) {
if (is_array($item)) {
if (!array_key_exists($key, $item)) {
throw new \LogicException("Could not map item by key \"{$key}\". Array key does not exist.");
}
return $item[$key];
}
if (!is_object($item)) {
throw new \InvalidArgumentException("Item must be an array or object.");
}
$ref = new \ReflectionClass($item);
if ($ref->hasProperty($key) && $ref->getProperty($key)->isPublic()) {
return $item->{$key};
}
if ($ref->hasMethod($key) && !$ref->getMethod($key)->isPrivate()) {
return $item->{$key}();
}
$get = 'get' . ucfirst($key);
if ($ref->hasMethod($get) && !$ref->getMethod($get)->isPrivate()) {
return $item->{$get}();
}
$is = 'is' . ucfirst($key);
if ($ref->hasMethod($is) && !$ref->getMethod($is)->isPrivate()) {
return $item->{$is}();
}
throw new \LogicException("Could not map item by key \"{$key}\". Cannot access the property directly or through getter/is method.");
}, $input);
}
示例3: callHooks
/**
* Call the initialization hooks.
*
* @param \Pimple $container The container that got initialized.
*
* @return void
*
* @throws \InvalidArgumentException When the hook method is invalid.
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
protected function callHooks($container)
{
if (isset($GLOBALS['TL_HOOKS']['initializeDependencyContainer']) && is_array($GLOBALS['TL_HOOKS']['initializeDependencyContainer'])) {
foreach ($GLOBALS['TL_HOOKS']['initializeDependencyContainer'] as $callback) {
if (is_array($callback)) {
$class = new \ReflectionClass($callback[0]);
if (!$class->hasMethod($callback[1])) {
if ($class->hasMethod('__call')) {
$method = $class->getMethod('__call');
$args = array($callback[1], $container);
} else {
throw new \InvalidArgumentException(sprintf('No such Method %s::%s', $callback[0], $callback[1]));
}
} else {
$method = $class->getMethod($callback[1]);
$args = array($container);
}
$object = null;
if (!$method->isStatic()) {
$object = $this->getInstanceOf($callback[0]);
}
$method->invokeArgs($object, $args);
} else {
call_user_func($callback, $container);
}
}
}
}
示例4: testContracts
public function testContracts()
{
$this->assertTrue($this->reflectedObject->isAbstract());
$this->assertTrue($this->reflectedObject->hasMethod('cleanVar'));
$this->assertTrue($this->reflectedObject->hasMethod('getVar'));
$this->assertInstanceOf('\\Xoops\\Core\\Text\\Sanitizer', \PHPUnit_Framework_Assert::readAttribute($this->object, 'ts'));
}
示例5: testContracts
public function testContracts()
{
$this->assertTrue($this->reflectedObject->isAbstract());
$this->assertTrue($this->reflectedObject->hasMethod('getDhtmlEditorSupport'));
$this->assertTrue($this->reflectedObject->hasMethod('registerExtensionProcessing'));
$this->assertTrue($this->reflectedObject->hasMethod('getEditorButtonHtml'));
}
示例6: testHasMethods
public function testHasMethods()
{
$reflection = new \ReflectionClass('\\Cekurte\\Environment\\Resource\\ResourceInterface');
$this->assertTrue($reflection->hasMethod('setResource'));
$this->assertTrue($reflection->hasMethod('getResource'));
$this->assertTrue($reflection->hasMethod('process'));
}
示例7: load
public function load(Application $application)
{
$in = $this->container->getParameter('seed.directory');
//add seed:load and seed:unload commands
$application->add($this->container->get('seed.load_seeds_command'));
$application->add($this->container->get('seed.unload_seeds_command'));
//Go through bundles and add *Seeds available in seed.directory
foreach ($application->getKernel()->getBundles() as $bundle) {
if (!is_dir($dir = sprintf('%s/%s', $bundle->getPath(), $in))) {
continue;
}
$finder = new Finder();
$finder->files()->name('*Seed.php')->in($dir);
$prefix = $bundle->getNameSpace() . '\\' . strtr($in, '/', '\\');
foreach ($finder as $file) {
$ns = $prefix;
if ($relativePath = $file->getRelativePath()) {
$ns .= '\\' . strtr($relativePath, '/', '\\');
}
$class = $ns . '\\' . $file->getBasename('.php');
$alias = 'seed.command.' . strtolower(str_replace('\\', '_', $class));
if ($this->container->has($alias)) {
continue;
}
$r = new \ReflectionClass($class);
if ($r->isSubclassOf('Soyuka\\SeedBundle\\Command\\Seed') && !$r->isAbstract() && ($r->hasMethod('load') || $r->hasMethod('unload'))) {
$application->add($r->newInstanceArgs([$this->prefix, $this->separator]));
}
}
}
}
示例8: initializeModels
/**
* Initializes the EventDispatcher-aware models.
*
* This methods has to accept unknown classes as it is triggered during
* the boot and so will be called before running the propel:build command.
*/
public function initializeModels()
{
foreach ($this->classes as $id => $class) {
$baseClass = sprintf('%s\\om\\Base%s', substr($class, 0, strrpos($class, '\\')), substr($class, strrpos($class, '\\') + 1, strlen($class)));
try {
$ref = new \ReflectionClass($baseClass);
} catch (\ReflectionException $e) {
$this->log(sprintf('The class "%s" does not exist.', $baseClass));
continue;
}
try {
$ref = new \ReflectionClass($class);
} catch (\ReflectionException $e) {
$this->log(sprintf('The class "%s" does not exist. Either your model is not generated yet or you have an error in your listener configuration.', $class));
continue;
}
$canSetEventDispatcher = false;
if (strpos($class, 'Query') !== false && $ref->hasMethod('setEventDispatcher') && $ref->hasMethod('getEventDispatcher')) {
$canSetEventDispatcher = true;
} elseif (strpos($class, 'Query') !== false) {
$this->log(sprintf('The class "%s" does not implement "%s" and "%s" methods.', $class, 'setEventDispatcher', 'getEventDispatcher'));
continue;
}
if (!$canSetEventDispatcher && !$ref->implementsInterface(self::MODEL_INTERFACE)) {
$this->log(sprintf('The class "%s" does not implement "%s". Either your model is outdated or you forgot to add the EventDispatcherBehavior.', $class, self::MODEL_INTERFACE));
continue;
}
$class::setEventDispatcher(new LazyEventDispatcher($this->container, $id));
}
}
示例9: process
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('mautic.model') as $id => $tags) {
$definition = $container->findDefinition($id);
$definition->addMethodCall('setEntityManager', [new Reference('doctrine.orm.entity_manager')]);
$definition->addMethodCall('setSecurity', [new Reference('mautic.security')]);
$definition->addMethodCall('setDispatcher', [new Reference('event_dispatcher')]);
$definition->addMethodCall('setTranslator', [new Reference('translator')]);
$definition->addMethodCall('setUserHelper', [new Reference('mautic.helper.user')]);
$modelClass = $definition->getClass();
$reflected = new \ReflectionClass($modelClass);
if ($reflected->hasMethod('setRouter')) {
$definition->addMethodCall('setRouter', [new Reference('router')]);
}
if ($reflected->hasMethod('setLogger')) {
$definition->addMethodCall('setLogger', [new Reference('monolog.logger.mautic')]);
}
if ($reflected->hasMethod('setSession')) {
$definition->addMethodCall('setSession', [new Reference('session')]);
}
// Temporary, for development purposes
if ($reflected->hasProperty('factory')) {
$definition->addMethodCall('setFactory', [new Reference('mautic.factory')]);
}
}
}
示例10: validateCallback
/**
* Validates a callback more strictly and with more detailed errors.
*
* @param string|object|array $class A class name, object, function name, or array containing class/object and method
* @param null|string $method If first param is class or object, the method name
* @param string $error Error key returned by reference
* @param bool $forceMethod If true, if no method is provided, never treat the class as a function
*
* @return bool
*
* @throws InvalidArgumentException
*/
public static function validateCallback($class, $method = null, &$error = null, $forceMethod = true)
{
if (is_array($class)) {
if ($method) {
throw new InvalidArgumentException('Method cannot be provided with class as array');
}
$method = $class[1];
$class = $class[0];
}
if ($forceMethod) {
$method = strval($method);
} else {
if (!$method) {
if (is_object($class)) {
throw new InvalidArgumentException('Object given with no method');
}
if (!function_exists($class)) {
$error = 'invalid_function';
return false;
} else {
return true;
}
}
}
if (!is_string($method)) {
throw new InvalidArgumentException('Method to check is not a string');
}
if (!is_object($class)) {
if (!$class || !class_exists($class)) {
$error = 'invalid_class';
return false;
}
}
$reflectionClass = new ReflectionClass($class);
$isObject = is_object($class);
if ($isObject && $reflectionClass->hasMethod('__call') || !$isObject && $reflectionClass->hasMethod('__callStatic')) {
// magic method will always be called if a method can't be
return true;
}
if (!$method || !$reflectionClass->hasMethod($method)) {
$error = 'invalid_method';
return false;
}
$reflectionMethod = $reflectionClass->getMethod($method);
if ($reflectionMethod->isAbstract() || !$reflectionMethod->isPublic()) {
$error = 'invalid_method_configuration';
return false;
}
$isStatic = $reflectionMethod->isStatic();
if ($isStatic && $isObject) {
$error = 'method_static';
return false;
} else {
if (!$isStatic && !$isObject) {
$error = 'method_not_static';
return false;
}
}
return true;
}
示例11: testAddMethod
/**
*/
public function testAddMethod()
{
$method = new ReflectionMethod('testMethod');
$this->assertFalse($this->object->hasMethod('testMethod'));
$this->object->addMethod($method);
$this->assertTrue($this->object->hasMethod('testMethod'));
}
示例12: invoke
public function invoke(URLComponent $url)
{
$class_name = $url->getController();
$method = $url->getAction();
$class = $this->app . '\\action\\' . $class_name;
//Response对象
$response = Response::getInstance();
//Request对象
$request = Request::getInstance();
#实例化控制器,使用反射
$reflection = new \ReflectionClass($class);
$instacne = $reflection->newInstance();
//先执行初始化方法init
if ($reflection->hasMethod('init')) {
$init = $reflection->getMethod('init');
$data = $init->invokeArgs($instacne, array($request, $response));
if ($data) {
//如果有返回数据则输出
$response->setBody($data);
$response->send();
return true;
}
}
if ($reflection->hasMethod($method)) {
$method = $reflection->getMethod($method);
} elseif ($reflection->hasMethod('getMiss')) {
$method = $reflection->getMethod('getMiss');
} else {
throw new RouteException('Method does not exist.');
}
$data = $method->invokeArgs($instacne, array($request, $response));
#输出
$response->setBody($data);
$response->send();
}
示例13: make
public static function make($name, $param = [])
{
$name = strtolower($name);
if (isset(self::$wareHouse[$name . join(":", $param)])) {
return self::$wareHouse[$name . join(":", $param)];
}
if (!($service = Dependency::getService($name))) {
Log::Error("The service \"{$name}\" dose not exist!");
throw new \Exception("The service \"{$name}\" dose not exist!", 1);
}
if (!class_exists($service)) {
Log::Error("Class \" {$service}\" dose not exist!");
throw new \Exception("Class \" {$service}\" dose not exist!", 1);
}
$reflectClass = new \ReflectionClass($service);
if ($reflectClass->hasMethod("newInstance")) {
if (empty($param)) {
return self::$wareHouse[$name] = call_user_func([$service, "newInstance"]);
} else {
return self::$wareHouse[$name . join(":", $param)] = call_user_func_array([$service, "newInstance"], $param);
}
}
if ($constructor = $reflectClass->hasMethod("__construct")) {
if ($constructor->isPublic()) {
return self::$wareHouse[$name . join(":", $param)] = $reflectClass->newInstanceArgs($param);
}
}
throw new \Exception("Class \" {$service} \" has no public contructor ", 1);
}
示例14: testGetterAndSetter
/**
* @dataProvider getClass
*/
public function testGetterAndSetter($class, $constructorParameterList)
{
$reflexionClass = new \ReflectionClass($class);
if ($reflexionClass->getConstructor()) {
$instance = $reflexionClass->newInstanceArgs($constructorParameterList);
} else {
$instance = $reflexionClass->newInstance();
}
$propertyList = $reflexionClass->getProperties();
foreach ($propertyList as $property) {
$name = ucfirst($property->getName());
$property->setAccessible(true);
$getter = 'get' . $name;
$setter = 'set' . $name;
if ($reflexionClass->hasMethod($setter)) {
$type = $this->guessParamType($reflexionClass->getMethod($setter));
if (isset($this->valueList[$type])) {
$value = $this->valueList[$type];
$instance->{$setter}($value[0]);
// test setter
$this->assertEquals($property->getValue($instance), $value[0]);
if ($reflexionClass->hasMethod($getter)) {
$property->setValue($instance, $value[1]);
// test getter
$this->assertEquals($instance->{$getter}(), $value[1]);
}
}
}
}
}
示例15: initialize
/**
* Initialize preferences
*
* @access public
* @param array
* @return void
*/
public function initialize(array $config = array(), $reset = TRUE)
{
$reflection = new ReflectionClass($this);
if ($reset === TRUE) {
$defaults = $reflection->getDefaultProperties();
foreach (array_keys($defaults) as $key) {
if ($key[0] === '_') {
continue;
}
if (isset($config[$key])) {
if ($reflection->hasMethod('set_' . $key)) {
$this->{'set_' . $key}($config[$key]);
} else {
$this->{$key} = $config[$key];
}
} else {
$this->{$key} = $defaults[$key];
}
}
} else {
foreach ($config as $key => &$value) {
if ($key[0] !== '_' && $reflection->hasProperty($key)) {
if ($reflection->hasMethod('set_' . $key)) {
$this->{'set_' . $key}($value);
} else {
$this->{$key} = $value;
}
}
}
}
// if a file_name was provided in the config, use it instead of the user input
// supplied file name for all uploads until initialized again
$this->_file_name_override = $this->file_name;
return $this;
}