"PHP" Object-oriented (i)

Source: Internet
Author: User
Tags null null

1. Learn about object-oriented goals:
A) Study of grammar:
b) Learning of programming ideas:
I. Process:
Ii. Object-oriented:
2. Comparison (with the difference between object and no object)
A) No object:
I'm hungry, I cook for myself.
II. I'm thirsty for boiling water.
III. Clothes are dirty to wash clothes
Iv. empty and lonely to play lol
b) There are objects
I. I'm hungry, I'm going to cook.
II. I'm thirsty, and I'm going to boil water.
III. My clothes are dirty, I'm going to wash my clothes.
Iv. I am empty and lonely object play lol game with me
c) Object-oriented core, is to let the object to help us to implement the specified function
3. Object: A single thing with certain functions and characteristics, called an object
A) object is actually a thing that exists
4. Class: An abstraction of an object with the same functionality and characteristics, called a class
5. Relationship of classes and objects:
A) A class is manifested (instantiated) as an object
B) Object abstraction is a class
6. Analogy:
A) The people who come out of the picture Mariang Mariang painting
b) Programmer Class object
C) Designer-designed drawings constructed from design drafts
7. How to declare a class: (equivalent to designing a drawing)
A) Format: class Name {}
b) Name: The first capital of the hump naming method named
c) Identifier naming rules: Letters, numbers, underscores, initials cannot be numeric, are strictly case-sensitive, and cannot use keywords
d) declaring member properties:
I. Note:
1. Must be given a member modifier (public, Protected, Private)
2. The default value of the member property (the initial value) can be
3. When the member attribute is assigned an initial value
A) You cannot use a variable to assign a value
b) It is not recommended to use calculations to assign values to member properties
c) You cannot assign a value to a member property by using a function call method
4. The data type of the variable:
A) four types of scalars: integer, Float, String, Boolean
b) Two kinds of Composition: object, array
c) Two kinds of special: resources, NULL NULL
5. Constants can be stored in the data type:
A) 4 winning volume type and NULL NULL type
e) Declaration of the Member method:
I. Note
1. The member method is a function, which we call the method in the class.
2. Similar to the way member properties are declared, the member method also needs to be given a member modifier (public, protected, private)
3. The parameters of the member method are optional
4. The return value of the member method and the code body are optional!
5. function when we do not specify return, or, given a return but no value, the result is null
f) There can only be member properties and member methods in the class, the contents will be error!
g) The position of member properties and member methods is not fixed, but it is recommended that member properties be written in front
8. Instantiation of the object:
A) Format: variable name =new class name ()

Object = type of variable
(MEINV) = class name
[1] = = index number of the object being instantiated in the current script

1 //Declare a beauty class (equivalent to the design process)2 classmeinv{3     4     //feature = = member Property5      Public $name;6      Public $sex;7      Public $age=18;8      Public $tall;9     Ten     //function = Member Method One      Public functioneat () { A         Echo' I'm eating! ‘; -     } -      the      Public functiondrink () { -         Echo' I'm drinking. '; -     } -      +      Public functionTalk$someone){ -         Echo' I am and '.$someone.‘ Speak! ‘; +     } A } at  - //instantiation (materialized) - $lingling=NewMEINV (); -  - //operation of member properties - $lingling->name = ' lingling '; in $lingling->sex = ' female '; - $lingling->age = 20; to $lingling->tall = 160.00; +  - Var_dump($lingling); the  * //Results $ Object(MEINV) [1]Panax Notoginseng    Public' Name ' + =string' Lingling ' (length=6) -    Public' Sex ' =string' Woman ' (length=3) the    Public' Age ' = int 20 +    Public' Tall ' =float160

b) Note:
I. The parentheses after the class name are optional, but we recommend adding
Ii. instantiation of an object can precede the declaration of a class, or after the declaration of a class
III. member Accessors:

1 //declaring a mobile phone class2 3 classphone{4     5     //member Properties6      Public $brand;7      Public $size;8      Public $color;9     Ten     //member Methods One      Public functionCall$someone){ A         Echo' I'm giving '.$someone.‘ Call <br/> '; -     } -      the      Public functionSendMessage ($someone){ -         Echo' I'm giving '.$someone.‘ Send SMS <br/> '; -     } -      + } -  + //Instantiating Classes A $apple=NewPhone (); at  - //Initializing member Property Information - $apple->brand = ' Apple '; - $apple->size = ' 4.7inch '; - $apple->color = ' Red '; -  in //working with member properties - ///Add the member properties to     $apple->price = ' 5799 yuan '; +     //deletion of member properties - //unset ($apple->color); the //Modification of member properties *     $apple->size = ' 5.5inch '; $     //access to member propertiesPanax Notoginseng     Echo $apple->brand;//Apple -     Echo $apple->size;//5.5inch the     Echo $apple->price;//5799 USD +      A //Actions for member methods the $apple->call (' Fan Bingbing ');//I'm talking to Fan Bingbing . + $apple->sendmessage (' Li Bingbing ');//I'm texting Li Bingbing .

9. $this Special Variables
A) Special name
b) can only be used in member methods within the class
c) $this represents the object that is accessing the current method
10. Construction Method:
A) Essence: Also a member method, is a special member method
b) Name: __construct, and the name of the class name method are constructed method, we recommend the use of __construct;
c) Execution scenario: When we instantiate a class, the __construct method is called automatically
d) Function: Initialize Object
11. Destruction Method:
A) Essence: Also a member method, is a special member method
b) Name: __destruct
c) Execution scenario: automatically executes when our object is destroyed

1 //declare a car class2 classcar{3     4     //member Properties5      Public $brand;6      Public $type;7      Public $price;8     9     //member MethodsTen      Public function__construct ($brand,$type,$price){ One         //Assigning a value to a member property A         $this->brand =$brand; -         $this->type =$type; -         $this->price =$price; the          -         Echo $this->brand. ' Create <br/> '; -     } -      +      Public functionDrive () { -         Echo' I'm driving, aren't I?! ‘; +     } A      at      Public functionCarry$someone){ -         Echo' I'm carrying '.$someone.‘ Find a girlfriend! ‘; -     } -      -     //destructor, which is automatically executed when our script finishes executing -      Public function__destruct () { in         Echo $this->brand. ' Destruction <br/> '; -     } to      + } -  the //instantiation of a Mercedes Benz * $benz=NewCar (' Mercedes ', ' Off-road ', ' 200w '); $ Panax Notoginseng $baoma=NewCar (' BMW ', ' hood ', ' 180w '); -  the //Results + Mercedes-Benz Creation A BMW created the BMW destroyed +Mercedes destruction

"PHP" Object-oriented (i)

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.