The first article in the object-oriented series of browser PHP-class and object-oriented first article
× Contents [1] class [2] member attributes [3] member method [4] object [5] member access before [6] this
Object-Oriented Programming (OOP) is a computer programming architecture. A computer program consists of a single unit or object that can act as a subroutine. To achieve the overall operation, each object can receive, process, and send information to other objects. OOP has achieved three goals of Software Engineering: reusability, flexibility, and scalability, making programming code more concise, easier to maintain, and more reusable. PHP implements object-oriented programming through classes and objects. This article will introduce php classes and objects in detail.
Understanding PHP classes and objects can help you better understand javascript inheritance.
Class
The relationship between a class and an object is like the relationship between a mold and a casting. The instantiation result of a class is an object, and the abstraction of an object is a class. Class describes a group of objects with the same features (attributes) and the same behavior (methods. During development, the abstract class should be used to create an object, but the object rather than the class should be used directly in the program.
A class is an independent program unit and a set of objects with the same attributes and services. It provides a unified abstract description for all objects of this class, which includes two main parts: Member attributes and service methods.
The definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly brackets, which contains the class attributes and method definitions.
A class can contain its own constants, variables (called "attributes"), and functions (called "methods ")
<? Phpclass SimpleClass {// member attribute public $ var = 'a default value'; // member method public function displayVar () {echo $ this-> var ;}}?>
Member attributes
The variable member of the class is called "attribute", or "field" and "feature ". Attribute Declaration starts with the keyword public, protected, or private, and then starts with a common variable declaration. The variables in the attribute can be initialized, but the initialization value must be the final value, instead of the expressions, variables, methods, or function calls with operators.
If you directly declare an attribute using var instead of public, protected, or private, PHP5 regards it as public
Public $ var3 = 1 + 2; // The error format is public $ var4 = self: myStaticMethod (); // The error format is public $ var5 = $ myVar; // incorrect format: public $ var6 = 100; // common value (four scalar: integer, floating point, Boolean, string) public $ var6 = myConstant; // constant public $ var7 = self: classConstant; // static attribute public $ var8 = array (true, false); // array
Member Method
[Modifier] function Method Name (parameter...) {[method body] [return value]}
The modifier of member methods include public, protected, private, static, abstract, and final.
The declared member methods must be related to objects and cannot be meaningless operations.
Public function say () {echo "speak";} public function run () {echo "walk ";}
Object
In the objective world, all things are composed of links between objects. An object is an entity used to describe objective things in a system. It is a basic unit of a system.
To create an object, that is, to instantiate a class, the new keyword must be used.
If new is followed by a string containing the class name, an instance of the class will also be created.
<? Php $ instance = new SimpleClass (); // You can also do this: $ className = 'foo'; $ instance = new $ className (); // Foo ()?>
Member access
The class contains the member attributes and member methods. You can use the "new" keyword to create an object, that is, $ reference name = new Class Name (construction parameter ); you can use the special operator "->" to access the member attributes or member methods of an object.
$ Reference name = new Class Name (construction parameter); $ reference name-> member attribute = value assignment; // object attribute value echo $ reference name-> member attribute; // output object attributes $ reference name-> member method (parameter); // call the object Method
[Note] If the Members in the object are not static, this is the only access method.
When you assign an instance created by an object to a new variable, the new variable accesses the same instance, which is the same as the value assigned by this object. This behavior is the same as when the function is passed into the instance. You can create a new instance by cloning it to a created object.
<?phpclass SimpleClass{}$instance = new SimpleClass();$assigned = $instance;$reference =& $instance;$instance->var = '$assigned will have this value';$instance = null; var_dump($instance);//nullvar_dump($reference);//null/*object(SimpleClass)[1] public 'var' => string '$assigned will have this value' (length=30) */var_dump($assigned);?>
This
When a method is called within the class definition, there is an available Pseudo Variable this. the reference of a special object this is a reference to this object in the member methods of the object, but it can only be used in the object member method, whether it is inside the object using $ this to access the internal members of the object. Or access members of an object through the reference name of the object outside the object, the special operator "->" is required to complete the access.
<?phpclass A{ function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } }}class B{ function bar() { // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); }}$a = new A();$a->foo();//$this is defined (A) A::foo();//$this is not defined. $b = new B();$b->bar();//$this is defined (B) B::bar();//$this is not defined.?>