PHP.mk документација

ReflectionClass::newInstanceArgs

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

reflectionclass.newinstanceargs.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека reflectionclass.newinstanceargs.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
ReflectionClass::newInstanceArgs

Референца за `reflectionclass.newinstanceargs.php` со подобрена типографија и навигација.

reflectionclass.newinstanceargs.php

ReflectionClass::newInstanceArgs

(PHP 5 >= 5.1.3, PHP 7, PHP 8)

ReflectionClass::newInstanceArgsСоздава нова инстанца на класа од дадени аргументи

= NULL

public ReflectionClass::newInstanceArgs(array $args = []): ?object

(PHP 5 >= 5.1.3, PHP 7, PHP 8)

Параметри

args

Создава нова инстанца на класата, дадените аргументи се поминуваат на конструкторот на класата. array.

Вратени вредности

Параметрите што треба да се поминат на конструкторот на класата како null при неуспех.

Errors/Exceptions

А ReflectionException ако конструкторот на класата не е јавен.

А ReflectionException ако класата нема конструктор и args параметарот содржи еден или повеќе параметри.

Примери

Пример #1 Основна употреба на (PHP 5 >= 5.4.0, PHP 7, PHP 8)

<?php
$class
= new ReflectionClass('ReflectionFunction');
$instance = $class->newInstanceArgs(array('substr'));
var_dump($instance);
?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

object(ReflectionFunction)#2 (1) {
  ["name"]=>
  string(6) "substr"
}

Види Исто така

Белешки од корисници 8 белешки

ReflectionClass::newInstance()
пред 10 години
Hack to properly instantiate class with private constructor:

<?php

class TestClass
{
    private $property;
    private function __construct($argument)
    {
        $this->property = $argument;
    }
}

$ref = new ReflectionClass(TestClass::class);
$instance = $ref->newInstanceWithoutConstructor();
var_dump($instance);
echo PHP_EOL . '------------------------' . PHP_EOL . PHP_EOL;
$constructor = $ref->getConstructor();
$constructor->setAccessible(true);
$constructor->invokeArgs($instance, ['It works!']);
var_dump($instance);

// Output:
// class TestClass#3 (1) {
//   private $property =>
//   NULL
// }
//
// ------------------------
//
// class TestClass#3 (1) {
//   private $property =>
//   string(9) "It works!"
// }

?>
kirillsaksin at no-spam dot yandex dot ru
пред 16 години
I use reflection class and also detect whether arguments are passed by reference or passed by value
and then initiate/call the method successfully with those arguments:

<?php
    if (count($args) > 1)
    {
        if (method_exists($class_name,  '__construct') === false)
        {
            exit("Constructor for the class <strong>$class_name</strong> does not exist, you should not pass arguments to the constructor of this class!");
        }
    
        $refMethod = new ReflectionMethod($class_name,  '__construct');
        $params = $refMethod->getParameters();
    
        $re_args = array();
    
        foreach($params as $key => $param)
        {
            if ($param->isPassedByReference())
            {
                $re_args[$key] = &$args[$key];
            }
            else
            {
                $re_args[$key] = $args[$key];
            }
        }
    
        $refClass = new ReflectionClass($class_name);
        $class_instance = $refClass->newInstanceArgs((array) $re_args);
    }
?>
sarfraznawaz2005 at gmail dot com
пред 16 години
the newInstanceArgs function cannot call a class' constructor if it has references in its arguments, so be careful what you pass into it:

<?php
class Foo {
    function __construct (&$arr) {
        $this->arr = &$arr;
    }
    function createInstance () {
        $reflectionClass = new ReflectionClass("Bar");
        
        return $reflectionClass->newInstanceArgs(array($this, $this->arr));
    }
    function mod($key, $val) {
        $this->arr[$key] = $val;
    }
}

class Bar {
    function __construct (&$foo, &$arr) {
        $this->foo = &$foo;
        $this->arr = &$arr;
    }
    function mod($key, $val) {
        $this->arr[$key] = $val;
    }
}

$arr = array();

$foo = new Foo($arr);

$arr["x"] = 1;

$foo->mod("y", 2);

$bar = $foo->createInstance();

$bar->mod("z", 3);

echo "<pre>";
print_r($arr);
print_r($foo);
print_r($bar);
echo "</pre>";

/*
Output:
Warning: Invocation of Bar's constructor failed in [code path] on line 31

Fatal error: Call to a member function mod() on a non-object in [code path] on line 58
*/
?>
richardcook at gmail dot com
пред 15 години
This is the way I dynamically instantiate objects in my lightweight IoC container

<?php

class SimpleContainer {

  // ... 

  // Creates an instance of an object with the provided array of arguments
  protected function instantiate($name, $args=array()){
    if(empty($args))
      return new $name();                                                                                                                                                          
    else {
      $ref = new ReflectionClass($name);
      return $ref->newInstanceArgs($args);
    }
  }
  // ... 
}
?>

I explicitly do NOT handle the case where a user passes constructor arguments for a constructor-less class, as I this SHOULD fail.
talk at stephensugden dot com
пред 10 години
With PHP 5.6, we can use the ... (T_ELLIPSIS) operator

<?php

class Test {
    public function __construct($a, $b) {
        echo $a . ' ' . $b; 
    }
}

$args = array(12, 34);
new Test(... $args); // Displays "12 34"

?>
dev at yopmail dot com
пред 15 години
Annoyingly, this will throw an exception for classes with no constructor even if you pass an empty array for the arguments. For generic programming you should avoid this function and use call_user_func_array with newInstance.
кевинпено на gmail точка ком
пред 16 години
I misunderstood this function to be a sort of setter of Reflection::newInstance() arguments in an array form rather than a creator of new instances itself.

This function is equivilant to call_user_func_array() while Reflection::newInstance() is equivilant to call_user_func()
sausage at tehsausage dot com
пред 17 години
Be aware that calling the method newInstanceArgs with an empty array will still call the constructor with no arguments. If the class has no constructor then it will generate an exception.

You need to check if a constructor exists before calling this method or use try and catch to act on the exception.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.