This code bellow gives me this error : Class 'MyNamespace\Database' not found
. How do I reference a class that belongs to no namespace, from inside one ?
Class Database
{
public function request()
{
}
}
namespace MyNamespace
{
class MyClass
{
public function myFuction()
{
Database::request();
}
}
}
$this::staticMethod(); Since PHP 5.3 you can use $var::method() to mean <class-of-$var>:: ; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method: self::staticMethod();
Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ).
The static variable is used in the A and B classes, yet the method is inherited so it is actually the same code which is called. Until PHP 8.0, there would be a distinct static variable depending on which class is called.
In PHP, we can have both static as well as non-static (instantiated) classes. Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both.
Try with
\Database::request();
Also see Namespace Basics Example 1 in the PHP Manual
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With