Ask for advice! How to deal with PHP Object-Oriented Learning

Source: Internet
Author: User
Ask for advice! PHP Object-Oriented Learning recently encountered some trouble in learning PHP object-oriented programming. I 'd like to give it a try. 1. use static variables in the class to reduce resource usage. Under what circumstances should static variables be used? What are the similarities and differences between it and global variables ?~ (This leaves me confused) ask for advice! PHP Object-Oriented Learning
Recently, I encountered some trouble in learning PHP object-oriented programming.

1. use static variables in the class to reduce resource usage. Under what circumstances should static variables be used? What are the similarities and differences between it and global variables ?~ (This makes me confused ).

2. constants in the class. What are the advantages and disadvantages of constants defined in classes and constants defined in non-Classes ?~ Under what circumstances do constants need to be defined ?~

3. is there any better PHP object-oriented instance for learning ?~ If yes, please provide related links. (Not basic learning, but a slightly deeper class instance)


Thank you .!!
------ Solution --------------------
PHP advanced programming: pattern, framework and test (PDF version of HD Chinese)
Http://www.verycd.com/topics/2783113/
------ Solution --------------------
Class, you can use static variables to reduce the use of resources. Under what circumstances should static variables be used? What are the similarities and differences between it and global variables?

Can reduce resource usage?

My understanding is (in two aspects ):
0. static variables of a class can be divided into "static variables of the class itself" (belong to the class), static variables of the class method (belong to the class, but have different definitions ).
1. (bucket) static variables belong to the entire class, which saves memory when instantiating many objects.
2. (computing time) addressing instance variables at the underlying layer takes less time than class static variables, but I don't know the static variables of class methods (answers from the experts ).
Under what circumstances?
1. (static variables of the class) for example, if you want to record the number of instances instantiated in a Person class, the static variables of the class will be satisfied. (For example, how many people are there in a place? add one when _ construct, and subtract one when __destruct)
2. (static variables of the method of the class) in this case, the main "context-related" (that is, the last execution result of this function will directly or indirectly affect the execution result, for example, four arithmetic operations ).
What are the similarities and differences between it and global variables?
Global variables are global as the name suggests. Object-oriented programming aims to achieve high cohesion and low coupling ". For example, for the total population $ count, if the global variable is used, the change will increase when the total number is met next time; sometimes the code may not be written by anyone else. if this variable is used by others, isn't it a cup of cake... However, if the class static variables are used, there will be no problem (at least each class has its own $ count)

Class. What are the advantages and disadvantages of constants defined in classes and constants defined in non-classes?
The difference between a constant defined in a class and a constant defined in a non-class is the scope of use, such as gender.
Person: GENDER_MALE = 'male'
Person: GENDER_FEMALE = 'femal'
Chicken: GENDER_MALE = 'male'
Chicken: GENDER_FEMALE = 'femal'
Cat: GENDER_MALE = 'xiong'
Cat: GENDER_FEMALE = 'femal'
If the two constants are defined globally, it is not so considerate.

Under what circumstances do constants need to be defined?
For example, gender of a person or female of an animal. If no constant is defined, 0 is used.
------ Solution --------------------
1, male
------ Solution --------------------
In case of male mistake, female becomes mela, isn't it a cup ?? In addition, if you want to change the 01 representation to male and female one day, you need to search 10 million lines of code. If you use constants, you just need to change the definition...
#! /Usr/bin/php
Class Person {

Public static $ count = 0; ### number of record class instances
Const GENDER_MALE = 'male ';
Const GENDER_FEMALE = 'female ';

Private $ _ gender = self: GENDER_MALE;
Private $ _ name = 'noname ';

Public function setName ($ name ){
$ This-> _ name = $ name;
Return $ this;
}

Public function _ construct ($ name = '', $ gender = self: GENDER_MALE ){
Self: $ count ++;
$ This-> _ name = $ name;
$ This-> _ genger = $ gender;
}

Public function _ destruct (){
Self: $ count --;
}

Public function calc ($ action = 'Add', $ num = 1 ){
Static $ sum = 0;

If ($ action = 'ADD '){
$ Sum + = $ num;
} Else if ($ action = 'sub '){
$ Sum-= $ num;

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.