当前位置: 首页>>代码示例>>PHP>>正文


PHP ReflectionClass::newInstanceWithoutConstructor方法代码示例

本文整理汇总了PHP中ReflectionClass::newInstanceWithoutConstructor方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::newInstanceWithoutConstructor方法的具体用法?PHP ReflectionClass::newInstanceWithoutConstructor怎么用?PHP ReflectionClass::newInstanceWithoutConstructor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ReflectionClass的用法示例。


在下文中一共展示了ReflectionClass::newInstanceWithoutConstructor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: create

 public function create($component, array $args = [], $forceNewInstance = false, $share = [])
 {
     if (!$forceNewInstance && isset($this->instances[$component])) {
         return $this->instances[$component];
     }
     if (empty($this->cache[$component])) {
         $rule = $this->getRule($component);
         $class = new \ReflectionClass($rule->instanceOf ? $rule->instanceOf : $component);
         $constructor = $class->getConstructor();
         $params = $constructor ? $this->getParams($constructor, $rule) : null;
         $this->cache[$component] = function ($args) use($component, $rule, $class, $constructor, $params, $share) {
             if ($rule->shared) {
                 if ($constructor) {
                     try {
                         $this->instances[$component] = $object = $class->newInstanceWithoutConstructor();
                         $constructor->invokeArgs($object, $params($args, $share));
                     } catch (\ReflectionException $r) {
                         $this->instances[$component] = $object = $class->newInstanceArgs($params($args, $share));
                     }
                 } else {
                     $this->instances[$component] = $object = $class->newInstanceWithoutConstructor();
                 }
             } else {
                 $object = $params ? $class->newInstanceArgs($params($args, $share)) : new $class->name();
             }
             if ($rule->call) {
                 foreach ($rule->call as $call) {
                     $class->getMethod($call[0])->invokeArgs($object, call_user_func($this->getParams($class->getMethod($call[0]), $rule), $this->expand($call[1])));
                 }
             }
             return $object;
         };
     }
     return $this->cache[$component]($args);
 }
开发者ID:kvox,项目名称:TVSS,代码行数:35,代码来源:dice.php

示例2: testGenerator

 public function testGenerator()
 {
     if (!self::$dbh) {
         $this->markTestSkipped('The mysql database isn\'t available, check config and server.');
     }
     $generator = new \Snok\EntityGenerator(self::$dbh, "Test\\Snok\\Entity", __DIR__ . "/Entity");
     $generator->generateAll();
     require_once __DIR__ . "/Entity/Address.php";
     $reflection = new \ReflectionClass("\\Test\\Snok\\Entity\\Address");
     $instance = $reflection->newInstanceWithoutConstructor();
     $this->setupEntity($instance);
     $instance->firstname = "John";
     $instance->lastname = "Logan";
     $instance->address = "123 Ground street";
     $instance->district = "Harlem";
     $instance->city = "Detroit";
     $instance->phone = "555-1234";
     $instance->commit();
     $instance2 = $reflection->newInstanceWithoutConstructor();
     $this->setupEntity($instance2);
     $this->assertNull($instance2->address_id);
     $this->assertNull($instance2->firstname);
     $instance2->address_id = $instance->address_id;
     $instance2->refresh();
     $this->assertEquals("John", $instance2->firstname, "When adding a name to an object with auto increment name should be returned");
     unlink(__DIR__ . "/Entity/Address.php");
 }
开发者ID:kalaspuffar,项目名称:snok,代码行数:27,代码来源:MysqlGeneratorTest.php

示例3: injectData

 /**
  * Injects the data in the class
  * @param  object $object The object to inject the data in
  * @return object The instanciated class with its loaded properties
  */
 public function injectData($object = null)
 {
     if ($object !== null) {
         $reflection = new \ReflectionClass($object);
         if ($reflection->getParentClass()->name !== $this->reflection->name) {
             throw new \LogicException('You can only inject data in a class of the same type');
         }
         $class = $object;
     } else {
         $reflection = $this->reflection;
         $class = $this->reflection->newInstanceWithoutConstructor();
     }
     foreach ($this->data as $key => $value) {
         $prop = $reflection->getProperty($key);
         $prop->setAccessible(true);
         // When the value given only is a reference, load it.
         if ($value instanceof Reference) {
             $value = $value->loadRef();
         } elseif (is_array($value)) {
             $value = self::injectDataInArray($value);
         }
         $prop->setValue($class, $value);
     }
     return $class;
 }
开发者ID:vierbergenlars,项目名称:gihp,代码行数:30,代码来源:Object.php

示例4: getInteractorName

 /**
  * Get the interactor name
  *
  * @return mixed
  *
  * @throws Exception\NonInteractorException
  */
 public function getInteractorName()
 {
     if (!$this->isInteractor()) {
         throw new NonInteractorException($this->file->getFilename());
     }
     $method = $this->reflection->getMethod(self::NAME_GETTER_METHOD);
     return $method->invoke($this->reflection->newInstanceWithoutConstructor(), self::NAME_GETTER_METHOD);
 }
开发者ID:mattdanbrown,项目名称:php-interactor,代码行数:15,代码来源:Candidate.php

示例5: createFromArray

 /**
  * @param array $array
  * @return static
  */
 public static function createFromArray(array $array)
 {
     $reflection = new \ReflectionClass(static::class);
     $object = $reflection->newInstanceWithoutConstructor();
     $object->fromArray($array);
     return $object;
 }
开发者ID:IvixLabs,项目名称:Common,代码行数:11,代码来源:ArrayableTrait.php

示例6: loadInformation

 /**
  * @param object $entity
  *
  * @return MetaInformation
  *
  * @throws \RuntimeException if no declaration for document found in $entity
  */
 public function loadInformation($entity)
 {
     $className = $this->getClass($entity);
     if (!is_object($entity)) {
         $reflectionClass = new \ReflectionClass($className);
         if (!$reflectionClass->isInstantiable()) {
             throw new \RuntimeException(sprintf('cannot instantiate entity %s', $className));
         }
         $entity = $reflectionClass->newInstanceWithoutConstructor();
     }
     if (!$this->annotationReader->hasDocumentDeclaration($entity)) {
         throw new \RuntimeException(sprintf('no declaration for document found in entity %s', $className));
     }
     $metaInformation = new MetaInformation();
     $metaInformation->setEntity($entity);
     $metaInformation->setClassName($className);
     $metaInformation->setDocumentName($this->getDocumentName($className));
     $metaInformation->setFieldMapping($this->annotationReader->getFieldMapping($entity));
     $metaInformation->setFields($this->annotationReader->getFields($entity));
     $metaInformation->setRepository($this->annotationReader->getRepository($entity));
     $metaInformation->setIdentifier($this->annotationReader->getIdentifier($entity));
     $metaInformation->setBoost($this->annotationReader->getEntityBoost($entity));
     $metaInformation->setSynchronizationCallback($this->annotationReader->getSynchronizationCallback($entity));
     $metaInformation->setIndex($this->annotationReader->getDocumentIndex($entity));
     $metaInformation->setIsDoctrineEntity($this->annotationReader->isDoctrineEntity($entity));
     return $metaInformation;
 }
开发者ID:zquintana,项目名称:SolrBundle,代码行数:34,代码来源:MetaInformationFactory.php

示例7: testKeyExists

 /**
  * @param $array
  * @param $key
  * @param $expected
  *
  * @dataProvider keyExistsProvider
  */
 public function testKeyExists($array, $key, $expected)
 {
     $method = self::$inst->getMethod('keyExists');
     $method->setAccessible(true);
     $actual = $method->invoke(self::$inst->newInstanceWithoutConstructor(), $key, $array);
     $this->assertEquals($expected, $actual);
 }
开发者ID:Ridzhi,项目名称:Smarrt,代码行数:14,代码来源:DotTest.php

示例8:

 /**
  * This is INSANE - don't put your actions in a constructor.
  */
 function test_actions_added_in_constructor()
 {
     $class = new \ReflectionClass('\\tenup\\demo\\Real_World_Class');
     $object = $class->newInstanceWithoutConstructor();
     \WP_Mock::expectActionAdded('init', [$object, 'action_init']);
     $object->__construct();
 }
开发者ID:ryanwelcher,项目名称:unit-test-wp-mock,代码行数:10,代码来源:Real_World_Tests.php

示例9: currentHHVM

 /**
  * Returns the current row of a result set under HHVM
  * The problem was fetch_object creates new instance of a given class,
  * and attaches resulted key/value pairs after the class was constructed.
  *
  * @return  mixed
  */
 private function currentHHVM()
 {
     if ($this->_as_object === TRUE or is_string($this->_as_object)) {
         if ($this->_reflect_class === NULL) {
             // Create reflection class of given classname or stdClass
             $this->_reflect_class = new ReflectionClass(is_string($this->_as_object) ? $this->_as_object : 'stdClass');
         }
         // Support ORM with loaded, when the class has __set and __construct if its ORM
         if ($this->_reflect_class->hasMethod('__set') === TRUE && $this->_reflect_class->hasMethod('__construct') === TRUE) {
             // Get row as associated array
             $row = $this->_result->fetch_assoc();
             // Get new instance without constructing it
             $object = $this->_reflect_class->newInstanceWithoutConstructor();
             foreach ($row as $column => $value) {
                 // Trigger the class setter
                 $object->__set($column, $value);
             }
             // Construct the class with no parameters
             $object->__construct(NULL);
             return $object;
         } elseif (is_string($this->_as_object)) {
             // Return an object of given class name
             return $this->_result->fetch_object($this->_as_object, (array) $this->_object_params);
         } else {
             // Return an stdClass
             return $this->_result->fetch_object();
         }
     }
     // Get row as associated array
     return $this->_result->fetch_assoc();
 }
开发者ID:MenZil-Team,项目名称:cms,代码行数:38,代码来源:Result.php

示例10: _checkProtection

 protected function _checkProtection($protectionType)
 {
     // Check type
     if (!is_string($protectionType) || $protectionType != self::PROTECTION_CSRF && $protectionType != self::PROTECTION_CAPTCHA && $protectionType != self::PROTECTION_BRUTEFORCE && $protectionType != self::PROTECTION_XSS) {
         throw new \Exception('Invalid protection type : "' . $protectionType . '"');
     }
     // Check class
     if (class_exists('framework\\security\\form\\' . ucfirst($protectionType))) {
         $className = 'framework\\security\\form\\' . ucfirst($protectionType);
     } else {
         $className = $protectionType;
     }
     $class = new \ReflectionClass($className);
     if (!in_array('framework\\security\\IForm', $class->getInterfaceNames())) {
         throw new \Exception('Form protection drivers must be implement framework\\security\\IForm');
     }
     if ($class->isAbstract()) {
         throw new \Exception('Form protection drivers must be not abstract class');
     }
     if ($class->isInterface()) {
         throw new \Exception('Form protection drivers must be not interface');
     }
     $classInstance = $class->newInstanceWithoutConstructor();
     $constuctor = new \ReflectionMethod($classInstance, '__construct');
     if ($constuctor->isPrivate() || $constuctor->isProtected()) {
         throw new \Exception('Protection constructor must be public');
     }
     return $className;
 }
开发者ID:eric-burel,项目名称:twentyparts,代码行数:29,代码来源:Form.class.php

示例11: revertObject

 /**
  * @param array $transformed
  * @param string $type
  * @return object
  * @throws \Exception
  */
 protected function revertObject($transformed, $type)
 {
     $class = new \ReflectionClass($type);
     $instance = $class->newInstanceWithoutConstructor();
     if (!is_array($transformed)) {
         throw new \Exception("Error while reverting [{$type}]. Input is not an array: " . ValuePrinter::serialize($transformed));
     }
     $reader = new PropertyReader($this->types, $class->getName());
     $properties = [];
     foreach ($reader->readState() as $property) {
         $properties[] = $property->name();
         if (array_key_exists($property->name(), $transformed)) {
             $value = new TypedValue($transformed[$property->name()], $property->type());
             $transformer = $this->transformers->toRevert($value);
             $property->set($instance, $transformer->revert($value));
         }
     }
     foreach ($transformed as $key => $value) {
         if (!in_array($key, $properties)) {
             $transformer = $this->transformers->toRevert($value);
             $instance->{$key} = $transformer->revert($value);
         }
     }
     return $instance;
 }
开发者ID:watoki,项目名称:stores,代码行数:31,代码来源:GenericObjectTransformer.php

示例12: _registerSingleton

 protected static function _registerSingleton($singleton, $singletonNumber, $parameters)
 {
     if (self::_isLocked($singleton)) {
         throw new \Exception('Loop singleton');
     }
     self::_lockSingleton($singleton);
     $num = $singletonNumber == null ? 0 : $singletonNumber;
     // No parameters, or empty, instance directly
     if (empty($parameters)) {
         self::$_singletons[$singleton][$num] = new $singleton();
     } else {
         // create reflectionClass instance, check if is abstract class
         $ref = new \ReflectionClass($singleton);
         if ($ref->isAbstract()) {
             throw new \Exception('Your singletoned class :"' . $singleton . '" must be non-abstract class');
         }
         // Instance, and check constructor, setAccessible if is not, for invoke
         $inst = $ref->newInstanceWithoutConstructor();
         $constuctor = new \ReflectionMethod($inst, '__construct');
         if ($constuctor->isPrivate() || $constuctor->isProtected()) {
             $constuctor->setAccessible(true);
         }
         //Finally invoke (with args) and asign object into singletons list
         if (!is_array($parameters)) {
             // Single parameter
             $constuctor->invoke($inst, $parameters);
         } else {
             $constuctor->invokeArgs($inst, $parameters);
         }
         self::$_singletons[$singleton][$num] = $inst;
     }
     self::_unLockSingleton($singleton);
 }
开发者ID:eric-burel,项目名称:twentyparts,代码行数:33,代码来源:Singleton.trait.php

示例13: create

 /**
  * 实例化验证规则
  * @param string $type
  * @param array $arguments
  * @throws UploadException
  * @return RuleInterface
  */
 public static function create($type, $arguments = null)
 {
     $ruleClass = '';
     switch ($type) {
         case static::RULE_SIZE:
             $ruleClass = 'SizeRule';
             break;
         case static::RULE_MIME_TYPE:
             $ruleClass = 'MimeTypeRule';
             break;
         case static::RULE_EXTENSION:
             $ruleClass = 'ExtensionRule';
             break;
         case static::RULE_SYSTEM:
             $ruleClass = 'SystemRule';
             break;
     }
     $ruleClass = "Slince\\Upload\\Rule\\{$ruleClass}";
     try {
         $ruleReflection = new \ReflectionClass($ruleClass);
         if ($ruleReflection->getConstructor() != null) {
             $instance = $ruleReflection->newInstanceArgs($arguments);
         } else {
             $instance = $ruleReflection->newInstanceWithoutConstructor();
         }
         return $instance;
     } catch (\ReflectionException $e) {
         throw new UploadException(sprintf('Rule "%s" does not support', $type));
     }
 }
开发者ID:slince,项目名称:upload,代码行数:37,代码来源:RuleFactory.php

示例14: lazyload

 /**
  * Create a ghost object.
  * 
  * @param array $values
  * @return static
  */
 public static function lazyload($values)
 {
     $class = get_called_class();
     if (is_scalar($values)) {
         if (!is_a($class, Identifiable::class, true)) {
             throw new \Exception("Unable to lazy load a scalar value for {$class}: Identity property not defined");
         }
         $prop = static::getIdProperty();
         if (is_array($prop)) {
             throw new \Exception("Unable to lazy load a scalar value for {$class}: Class has a complex identity");
         }
         $values = [$prop => $values];
     }
     $reflection = new \ReflectionClass($class);
     $entity = $reflection->newInstanceWithoutConstructor();
     foreach ((array) $entity as $prop => $value) {
         if ($prop[0] === "") {
             continue;
         }
         // Ignore private and protected properties
         unset($entity->{$prop});
     }
     foreach ($values as $key => $value) {
         $entity->{$key} = $value;
     }
     $entity->ghost__ = true;
     return $entity;
 }
开发者ID:Sarfaraaz,项目名称:db,代码行数:34,代码来源:Implementation.php

示例15: instance

 /**
  * Gets the Multiton instance referenced by key.
  *
  * Automatically creates an instance if non exists. If one instance already exists, the argument list is ignored.
  *
  * @param string $key      The key of the Multiton instance to get.
  * @param mixed  $args,... The arguments for creating the Multiton instance.
  *
  * @return Multiton The Multiton instance.
  */
 public static function instance($key = 'default')
 {
     if (!is_string($key) and !empty($key)) {
         throw new \InvalidArgumentException("The \$key must be a non-empty string.");
     }
     $class = static::$T;
     if (!class_exists($class)) {
         throw new Exceptions\ClassNotFoundException("The class {$class} was not found.");
     }
     if (!array_key_exists($class, static::$instances) or !array_key_exists($key, static::$instances[$class])) {
         if (!array_key_exists($class, static::$cache)) {
             static::$cache[$class]['forgeable'] = Utils::class_implements($class, 'axelitus\\Patterns\\Interfaces\\Forgeable');
         }
         $args = array_slice(func_get_args(), 1);
         if (static::$cache[$class]['forgeable']) {
             static::$instances[$class][$key] = call_user_func_array([$class, 'forge'], $args);
         } else {
             $ref = new \ReflectionClass($class);
             $ctor = $ref->getConstructor();
             $ctor->setAccessible(true);
             static::$instances[$class][$key] = $ref->newInstanceWithoutConstructor();
             $ctor->invokeArgs(static::$instances[$class][$key], $args);
         }
     }
     return static::$instances[$class][$key];
 }
开发者ID:boiler256,项目名称:php-design-patterns,代码行数:36,代码来源:TMultiton.php


注:本文中的ReflectionClass::newInstanceWithoutConstructor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。