"Code Learning" PHP Object-oriented and the like object

Source: Internet
Author: User

I. Relationship of classes and objects

The manifested result of a class is an object, and an object's abstraction is a class. In the development process, we usually first abstract (fantasy) out of a class, and then use the class to create objects (to achieve the illusion of content). In the program, we use the object of our (realization fantasy) directly, rather than the abstract (fantasy) class

Ii. object-oriented and process-oriented

1. What is process-oriented

Process-oriented is like a person to wash their own clothes, cooking, everything is to solve their own

2. What is Object-oriented

Object-oriented is that the person has the object, let the object to wash clothes, cook.

Iii. how to abstract a class

1 class class Name {2     member property:3         name, gender, age, height, weight 4     member Method:  5         talk, walk, study, eat, drive 6 }

All classes are declared from both the member property and the member method, and the properties and methods are members of the class, so the attribute is also called the member property, and the method is called the member method.

1. member Properties--Features

Declaring a variable directly in an object is called a member property, and you can declare multiple member variables in the class, that is, the object can have more than one member property

class person{    var$name;    // declares the first member property, which is used to store the name    of a person var age;      // declares the first member property, which is used to store the age    of a person var $sex;     // declares the first member property, which is used to store the gender of a person }

2. Member Method--function

classperson{var $name;//declares the first member property, which is used to store the name of a person    var $Age//declares the first member property, which is used to store the age of a person    var $sex;//declares the first member property, which is used to store the gender of a person    functionSay () {//declaration of the first method        Echo' People are talking ';//define the function of human speaking    }    functionRun () {//declaring a second method        Echo' People are running ';//define the function of people running    }    functionEat () {//declaring a third method        Echo' People are eating ';//define the function of people to eat}
}

The object is to organize the related properties and methods together. Where member properties and member methods are optional, you can have only member properties, you can have member methods, or you can have no members.

Iv. Instantiation of objects

A class can declare multiple objects, allowing objects to access member properties and Member methods

classperson{var $name;//declares the first member property, which is used to store the name of a person    var $age;//declares the first member property, which is used to store the age of a person    var $sex;//declares the first member property, which is used to store the gender of a person    functionSay () {//declaration of the first method        Echo' People are talking ';//define the function of human speaking    }    functionRun () {//declaring a second method        Echo' People are running ';//define the function of people running    }    functionEat () {//declaring a third method        Echo' People are eating ';//define the function of people to eat    }}//instantiating an object$sqyy=NewPerson ();//member Property Assignment value$sqyy-Name = "Yichuan";$sqyySex = "male";$sqyy-age = 26;//accessing member methods in an object$sqyy->eat ();

V. Special object Reference "$this"

In the above example, we instantiate an object and then assign a value to the member property and access the member method. Then $this can also be used in the member method, which represents the current object

Who is the object before the member method call, $this who

class person{    var$name;     function info () {        $this ->name = "Sqyy";    }

"Code Learning" PHP Object-oriented and the like object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.