Classes and objects->Basic concepts:
1, #### ########################
::class
Since PHP 5.5, the keyword class can also be used for class name resolution. Using ClassName::class you can get a string containing the fully qualified name of the class ClassName. This is especially useful for classes that use namespaces.
Example #7 Class name resolution as stated in the docs is:
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
The above routine will output:
NSClassName
2,################ ############
Just to be clear: the correct way of validating a classname, as stated in the docs is:
$valid = preg_match('/^[a-zA -Z_x7f-xff][a-zA-Z0-9_x7f-xff]*$/', $className);
3,################# ############
Variables in attributes can be initialized, but the initialized value must be a constant. The constant here means that the PHP script can get its value during the compilation phase without relying on Only runtime information can be evaluated
PHP 5.3.0 adds support for Nowdoc declaration class attributes; heredoc without variables is also possible, but it is wrong to include variables.
new static() will follow the inheritance relationship, new is the subclass
new self() will not be inherited, new is the class where the word self is located
###
As of PHP 5.6 you can finally define constant using math expressions, like this one:
class MyTimer {
const SEC_PER_DAY = 60 * 60 * 24;
}
?>
###
Since PHP 5.3.3, in the namespace, methods with the same name as the class name are no longer as a constructor. This change does not affect classes that are not in the namespace.
namespace Foo;
class Bar {
public $a;
public function Bar() {
$this->a = 'to here';
}
public function getA(){
return $this->a;
}
}
$bar = new Bar();
echo $bar- >getA(); //Empty; remove the namespace and output to here;
###
Since PHP 5.3.0, classes can be referenced through variables. The value of this variable cannot be keywords (such as self, parent and static).
With organization:
self, parent and static
public (public), protected (protected) or private (private)