PHP classes and objects
Since PHP 5, the object model has been completely rewritten to get better performance and more features. This is the biggest change since PHP 4. PHP 5 has a complete object model. New features in PHP 5 include access control, abstract classes and final classes and methods, additional magic methods, interfaces, object replication, and type constraints. PHP treats objects in the same way as references and handles, that is, each variable holds an object reference, rather than copying the entire object. Attribute: the variable member of the class is called the attribute declaration keyword. The variable in the public protected private property can be initialized, but the initial variable must be a constant and cannot be a computing formula such as private $ name = "tom "; // okprivate $ name = "tom ". "jack"; // constant in the error class: the constant value must be a fixed value, not a variable, class attribute, mathematical operator, function calls can only use const NAME = 'Tom '; the constant access constant is defined inside the class and the class: Case column is automatically loaded using classname: NAME outside the class:
Index. php file <? Phpheader ("content-type: text/html; charset = UTF-8"); function _ autoload ($ className) {require_once $ className. '. php ';} $ obj = new Name (); $ obj2 = new User (); var_dump ($ obj-> getName (); Name. PHP file <? Phpheader ("content-type: text/html; charset = UTF-8"); class Name {function getName () {return "Ouyang Jun" ;}} User. php file <? Phpheader ("content-type: text/html; charset = UTF-8"); class User {function getUser () {return array ('ouangjun', 'jpp ');}}