Voting

: max(two, three)?
(Example: nine)

The Note You're Voting On

thisleenoble at DOPEOPLESTILLNOSPAM dot me dot com
2 years ago
Instantiating an object with a string variable defaults to non-namespaced scope. Given two classes in the same namespace.

<?php
namespace foo;

class
bar {
  public function
createSubclass(string $type) {
     return new
$type();
  }
?>
<?php
namespace foo;

class
baz {

}
?>
<?php
$barObj
= new bar();
$barObj->createSubclass('baz');

// result: Uncaught Error: Class 'baz' not found
?>

Change bar class to:
<?php
namespace foo;

class
bar {
  public function
createSubclass(string $type) {
    
type = '\\'.__NAMESPACE__.'\\'.type;
     return new
$type();
  }
?>

<< Back to user notes page

To Top