I. Classes and objects
1, what is a class: class is a description of a class of things, is an abstract, conceptual definition. is a collection of objects with some of the same properties and functional behavior.
In object-oriented programming, a class should have a class name and include two main parts: the title of the property and the description of the function.
2. An object is an entity/instance used in the system to describe a customer's view of things.
3, the instantiation of the class result is the object, and the abstract of a class of objects is the classes. Class describes a set of objects that have the same attributes (attributes) and the same behavior (methods).
Ii. declarations of classes and creation of objects
1. Declaration of class
// declaring classes class class Name { //Code (Properties , Methods)...}
Specification: Class name capitalization, camel-like syntax
2. Create objects
$ object Variable New class name ();
Iii. properties and methods of classes
Property (also known as a member variable of a class), a common property in a class.
Declaring a property must be decorated with one of the public/private/protected to define the access rights for the variable.
Method (a member function of a class), the functional behavior in a class, by declaring a function in a class definition, that is, the method of the class that was created.
1. Access by reference variable (object)
First instantiate an object, $variable = new ClassName (), and then use the operator "-" to complete access to the object member properties and member methods.
Access properties: Objects, properties. "$variable->name;"
Modify property: Object--property = new value. "$variable->name = ' newName ';"
Access method: Object---method "$variable->method ();"
2. Intra-class access
Internal Access with keyword $this
Access properties: $this-Properties; "$this->name;"
Modify Property: $this-property = new value; "$this->name = ' newName ';"
Access method: $this-to method; "$this->method ();"
For example:
<?PHPclassperson{ Public $name;//Defining Properties Public $age;//Defining Properties Public functionSay () {//Create a method Echo' My name is '.$this->name. "And my age is".$this->age. " <br/> "; } Public functionSetName ($name= ' wuming ') {//ways to change the Age property $this->name =$name; } Public functionSetage ($age=18){ $this->age =$age; } } $person 1=NewPerson ();//instantiating the person class $person 1->setname (' Xiaozu '); Calling Methods$person 1->setage (24); Echo $person 1->say (); ?>
PHP Object-oriented (i)