本文整理汇总了PHP中ReflectionClass::newInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::newInstance方法的具体用法?PHP ReflectionClass::newInstance怎么用?PHP ReflectionClass::newInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
public function load()
{
$files = $this->_getFiles();
$manifestRegistry = ZendL_Tool_Rpc_Manifest_Registry::getInstance();
$providerRegistry = ZendL_Tool_Rpc_Provider_Registry::getInstance();
$classesLoadedBefore = get_declared_classes();
$oldLevel = error_reporting(E_ALL | ~E_STRICT);
// remove strict so that other packages wont throw warnings
foreach ($files as $file) {
require_once $file;
}
error_reporting($oldLevel);
// restore old error level
$classesLoadedAfter = get_declared_classes();
$loadedClasses = array_diff($classesLoadedAfter, $classesLoadedBefore);
foreach ($loadedClasses as $loadedClass) {
$reflectionClass = new ReflectionClass($loadedClass);
if ($reflectionClass->implementsInterface('ZendL_Tool_Rpc_Manifest_Interface') && !$reflectionClass->isAbstract()) {
$manifestRegistry->addManifest($reflectionClass->newInstance());
}
if ($reflectionClass->implementsInterface('ZendL_Tool_Rpc_Provider_Interface') && !$reflectionClass->isAbstract()) {
$providerRegistry->addProvider($reflectionClass->newInstance());
}
}
}
示例2: createEntity
/**
* @return object
*/
protected function createEntity()
{
if (!$this->entityClassReflection) {
$this->entityClassReflection = new \ReflectionClass($this->entityClassName);
}
return $this->entityClassReflection->newInstance();
}
示例3: display
public function display($url)
{
$data = (object) json_decode(file_get_contents('php://input'), true);
$reflection = new ReflectionClass($data->_type);
if ($reflection->isSubclassOf('DataSource')) {
$object = $reflection->newInstance();
$object->parameters()->{'root-element'} = 'data';
foreach ($data->_parameters as $key => $value) {
$object->parameters()->{$key} = $value;
}
$result = $object->render(new Register());
header('content-type: text/xml');
echo $result->saveXML();
exit;
}
if ($reflection->isSubclassOf('Event')) {
$object = $reflection->newInstance();
$object->parameters()->{'root-element'} = 'data';
foreach ($data->_parameters as $key => $value) {
$object->parameters()->{$key} = $value;
}
$result = $object->trigger(new Register(), (array) $data->_data);
header('content-type: text/xml');
echo $result->saveXML();
exit;
}
}
示例4: getModuleInstanceByTypeAndName
public static function getModuleInstanceByTypeAndName($sType, $sName)
{
if (!$sName) {
throw new Exception("Exception in Module::getModuleInstanceByTypeAndName(): module name is empty");
}
$sClassName = self::getClassNameByTypeAndName($sType, $sName);
if (!self::isModuleEnabled($sType, $sName)) {
throw new Exception("Exception in Module::getModuleInstanceByTypeAndName(): tried to instanciate disabled module {$sType}.{$sName}");
}
$aArgs = array_slice(func_get_args(), 2);
$oClass = new ReflectionClass($sClassName);
if ($sClassName::isSingleton()) {
if (!isset(self::$SINGLETONS[$sClassName])) {
try {
self::$SINGLETONS[$sClassName] = $oClass->newInstanceArgs($aArgs);
} catch (ReflectionException $ex) {
self::$SINGLETONS[$sClassName] = $oClass->newInstance();
}
}
return self::$SINGLETONS[$sClassName];
}
try {
return $oClass->newInstanceArgs($aArgs);
//Does not work in PHP < 5.1.3
} catch (ReflectionException $ex) {
return $oClass->newInstance();
//Does not work in PHP < 5.1.3
}
}
示例5: test
function test($class)
{
echo "====>{$class}\n";
try {
$ref = new ReflectionClass($class);
} catch (ReflectionException $e) {
var_dump($e->getMessage());
return;
// only here
}
echo "====>newInstance()\n";
try {
var_dump($ref->newInstance());
} catch (ReflectionException $e) {
var_dump($e->getMessage());
}
echo "====>newInstance(25)\n";
try {
var_dump($ref->newInstance(25));
} catch (ReflectionException $e) {
var_dump($e->getMessage());
}
echo "====>newInstance(25, 42)\n";
try {
var_dump($ref->newInstance(25, 42));
} catch (ReflectionException $e) {
var_dump($e->getMessage());
}
echo "\n";
}
示例6: createValidator
/**
* Create validator
*
* @param $validator
* @return \Phalcon\Validation\ValidatorInterface
* @throws \Engine\Exception
*/
public function createValidator($validator)
{
if ($validator instanceof ValidatorInterface) {
return $validator;
} elseif (is_array($validator)) {
$origName = ucfirst($validator['validator']);
if ($class = $this->getValidatorClassName($origName)) {
if (empty($validator['options'])) {
$validator = new $class();
} else {
$r = new \ReflectionClass($class);
if ($r->hasMethod('__construct')) {
$validator = $r->newInstance($validator['options']);
} else {
$validator = $r->newInstance();
}
}
}
} elseif (is_string($validator)) {
$origName = ucfirst($validator);
if ($class = $this->getValidatorClassName($origName)) {
$validator = new $class();
}
} else {
throw new \Engine\Exception("Validator '{$validator}' not exists");
}
return $validator;
}
示例7: invoke
/**
* @param array|null $args
*
* @return object
*/
function invoke(array $args = null)
{
if ($args !== null) {
return $this->reflectionClass->newInstanceArgs($args);
}
return $this->reflectionClass->newInstance();
}
示例8: testGetSingle
public function testGetSingle()
{
$singletags = ['br', 'clear', 'col', 'hr', 'xkcd'];
$method = $this->reflectionClass->getMethod('getSingle');
$this->parser = $this->reflectionClass->newInstance();
$this->assertEquals($singletags, $method->invoke($this->parser));
}
示例9: instantiateController
/**
*
* @param string $class
* @return BaseController
*/
protected function instantiateController($class)
{
$reflection = new \ReflectionClass($class);
if ($reflection->isSubclassOf('Tracker\\Controller\\BaseController')) {
return $reflection->newInstance($this->app);
}
return $reflection->newInstance();
}
示例10: getInst
/**
* Метод формирует экземпляр класса на основе строки.
* После будет применён фильтр, если он есть.
*
* @return object
*/
private function getInst(array $row)
{
if ($this->constructorParams) {
$args = $this->constructorParams;
$args[] = $row;
return $this->RC->newInstanceArgs($args);
} else {
return $this->RC->newInstance($row);
}
}
示例11: getChildren
/**
* @see \RecursiveIterator:getChildren();
*/
public function getChildren()
{
if ($this->current() instanceof self) {
return $this->current();
}
if (empty($this->ref)) {
$this->ref = new \ReflectionClass($this);
}
return $this->ref->newInstance($this->current() . '/');
}
示例12: createInstance
/**
* Creates a new instance for the class
*/
public static function createInstance($className, $args = [])
{
$reflection = new \ReflectionClass($className);
if ($reflection->implementsInterface('App\\Interfaces\\SingletonInterface')) {
if (isset(self::$singletones[$className])) {
return self::$singletones[$className];
} else {
$instance = $args ? $reflection->newInstanceArgs($args) : $reflection->newInstance();
self::$singletones[$className] = $instance;
}
} else {
$instance = $args ? $reflection->newInstanceArgs($args) : $reflection->newInstance();
}
return $instance;
}
示例13: get
/**
* @param mixed $id
* @param boolean $forceInstance Whether returns an empty instance when no result
*
* @return StaticEntity|null
*/
public function get($id, $forceInstance = false)
{
if (array_key_exists($id, $this->instances)) {
return $this->instances[$id];
} elseif (!($data = $this->getData($id))) {
return $forceInstance ? $this->reflection->newInstance() : null;
}
$this->instances[$id] = $this->reflection->newInstance();
foreach ($data as $property => $value) {
$reflectionProperty = $this->reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->instances[$id], $value);
}
return $this->instances[$id];
}
示例14: Deserialize
public function Deserialize($Root)
{
$result = null;
$counter = 0;
foreach ($Root as $member) {
$instance = new ReflectionClass($member->getName());
$ins = $instance->newInstance();
foreach ($member as $child) {
$rp = $instance->getMethod("set_" . $child->getName());
if (count($child->children()) == 0) {
$rp->invoke($ins, $child);
} else {
$rp->invoke($ins, $this->Deserialize($child->children()));
echo $child;
}
}
if (count($Root) == 1) {
return $ins;
} else {
$result[$counter] = $ins;
$counter++;
}
if ($counter == count($Root)) {
return $result;
}
}
}
示例15: instantiate
/**
* Instantiates objects by class and id, respecting pattern implemented by given class
*
* @param string $class Class name
* @param $id
* @return unknown_type
*/
public static function instantiate($class, $id = null)
{
if (!strlen($class)) {
require_once 'Oops/Exception.php';
throw new Oops_Exception("Empty class name given");
}
if (!Oops_Loader::find($class)) {
require_once 'Oops/Exception.php';
throw new Oops_Exception("Class '{$class}' not found");
}
$reflectionClass = new ReflectionClass($class);
if ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Factored_Interface')) {
/**
* Object can be instantiated using corresponding factory
*/
$factoryCallback = call_user_func($class, 'getFactoryCallback');
$result = call_user_func($factoryCallback, $id);
} elseif ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Singleton_Interface')) {
/**
* This object can be instantiated using $class::getInstance($id)
*/
$result = call_user_func(array($class, 'getInstance'), $id);
} elseif ($reflectionClass->implementsInterface('Oops_Pattern_Singleton_Interface')) {
/**
* This object is the single available instance of this class, so it can be instantiated using $class::getInstance()
*/
$result = call_user_func(array($class, 'getInstance'));
} else {
/**
* This type of object should be constructed with given $id
*/
$result = $reflectionClass->newInstance($id);
}
return $result;
}