So I found this problem:
// name1.php
namespace Examples\Names;
class Name {
public function __toString() {
return "Laurel";
}
public static function get() {
return "Eminent";
}
}
// name2.php
namespace Examples;
include "name1.php";
class Name {
public function __toString() {
return "James";
}
public static function get() {
return "Cook";
}
}
echo new Name() . " | " . Names\Name::get();
And I would like to understand why this echo echo new Name() . " | " . Names\Name::get();
take displays James | Eminent
.
I think that I got it about second second part Names\Name::get()
that it takes from Examples\Names\Name Eminent
because it has that get, but what about first part? Why it takes James
by default?
Also, when I try to remove switch from Names\Name::get()
to Names\Name()
just to see if it display __toString from second class - Cook
it doesn't work and I get this error Fatal error: Uncaught Error: Call to undefined function Examples\Names\Name() in C
Can you help me with that? I really want to understand. Thank you!
what about first part
...it uses the class from the Examples
namespace because it's in the same namespace as the echo
statement. And it echoes James
because of the __toString
- that serves as the default function called behind the scenes by PHP whenever you try to put an object into a string.
switch from Names\Name::get() to Names\Name()
... it would need to be new Names\Name()
I think, if you want to instantiate the class.