Ask for advice! PHP Object-Oriented Learning Solution

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 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. do you have any questions! 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 a constant is not defined, 0 | 1, male | female is used. if male is wrong, it turns into 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...
PHP code
#! /Usr/bin/php
  _ 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;} echo 'Now $ sum = ', $ sum, "\ n"; return $ this;} public function getGender () {return $ this-> _ gender;} public function setGender ($ gender = self: GENDER_MALE) {$ this-> _ gender = $ gender; }}$ ary = array (); foreach (range (1,100) as $ _) {$ ary [] = new Person ("Person $ _");} echo Person: $ count, "\ n "; ### 100 individual unset ($ ary [15]) ### destroy an echo Person: $ count, "\ n "; ### 99 individuals $ me = $ ary [0]-> setName ("me"); $ you = $ ary [1]-> setName ("you "); foreach (range (1, 3) as $ _) {### $ me-> calc ("add", $ _ * 2 ); $ you-> calc ("sub", $ _ * 3); echo "=========================\ n ";} echo $ me-> setGender (Person: GENDER_FEMALE); echo $ me-> getGender ();
------ Solution --------------------
Using static variables can reduce the use of resources ???
The difference between static attributes of a class and global variables is that they have different scopes.
When many objects are instantiated from a class, the static attributes are public data areas, that is, shared by each object.
This has nothing to do with resource consumption.

Similarly, class constants and constants only have different scopes.
Similarly, static methods of classes are a group of functions with limited scope of action.

Since php5.3 re-introduces the namespace concept, all the "static" things will be reset.

To learn object-oriented programming, you must use an object-oriented method to view the problem.
Do not be confused by the extremely bad terms translated.


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.