The new Class()
statement created a new object instance of class named Class
.
The Class::newInstance()
calls a static
method on class named Class
. Which in your tutorial most likley will call and return new Class()
.
The static function newInstance
needs to be present in the class. It is not native to all php objects afaik.
This should make it clear:
class Foo
{
private $bar = null;
public static function newInstance($args){
return new self($args);
}
public function __construct($bar = "nothing")
{
$this->bar = $bar;
}
public function foo()
{
echo "Foo says:" . $this->bar . "
";
}
}
//create using normal new Classname Syntax
$foo1 = new Foo("me");
$foo1->foo();
//create using ReflectionClass::newInstance
$rf = new ReflectionClass('Foo');
$foo2 = $rf->newInstance();
$foo2->foo();
//create using refelction and arguments
$foo3= $rf->newInstanceArgs(["happy"]);
$foo3->foo();
//create using static function
$foo4 = Foo::newInstance("static");
$foo4->foo();
Will output:
Foo says:me
Foo says:nothing
Foo says:happy
Foo says:static