For advice PHP Object-oriented learning

Source: Internet
Author: User
Tags parse error
Recently in the process of learning PHP object-oriented programming, encountered a bit of trouble, I would like to ask the great God to answer for me.

1, static variables in the class, using static variables can reduce the use of resources. So what is the use of static variables? What are the similarities and differences between it and global variables? ~ (This makes me very confused).

2, the constant in the class. What are the pros and cons of a constant defined in a class and a constant defined in a non-class? ~ What kind of situations do you need to define constants? ~

3, there is no good PHP object-oriented instance, you can learn it? ~ If yes, please provide the relevant link. (Not for basic learning, but for a slightly deeper class instance)


Here, thank you to the great God.!!


Reply to discussion (solution)

1. Global variables are all methods, functions, can be used, and a change, all change, and static variables generally exist in the class, such as there are A and B two classes, two classes can have a global variable cc, a under the static variable cc, does not affect the CC under B.

2. Constants have a characteristic, that is, the definition can not be changed, generally used to not change the place, compared to the location of the film, such a place, that is likely to change, but at least it is difficult to change the case.

3. The PHP Manual is more comprehensive, if you want to thoroughly learn, you can try C # or Java, and then to learn PHP, for object-oriented understanding you will be a duck to the fish.

Individuals are not very professional, there may be some areas of negligence, welcome to shoot bricks.

PHP Advanced Programming: Patterns, Frameworks and tests (HD Chinese PDF)
http://www.verycd.com/topics/2783113/

PHP Advanced Programming: Patterns, Frameworks and tests (HD Chinese PDF)
http://www.verycd.com/topics/2783113/
Thanks for sharing upstairs, download ....

Look at the PHP Bible will be, fragmented learning is not neat.

PHP manual Bar, the above examples are more, are targeted, try more

A static variable in a class that uses static variables to reduce the use of resources. So what is the use of static variables? What are the similarities and differences between it and global variables?

Can you reduce the use of resources?

My understanding is (in two ways):
0. Static variables of a class are divided into "static variables of the class itself" (belonging to the Class), static variables of the class method (belonging to the class, but not defined in the same location).
1. (storage space) static variable it belongs to the entire class, so that when you instantiate many objects, you can save memory.
2. (Operation time) Addressing instance variables from the bottom takes less time than class-static variables, but static variables for class methods do not know (the Bulls help answer them).
What is the use of?
1. (Static variables that belong to a class) such as a person class you want to record how many instances are instantiated then the static variables of the class can be satisfied. (such as how many people are in a place: add one when __construct, and subtract one when __destruct)
2. (static variable belonging to the method of the Class) This situation is mainly "context-sensitive" (that is, the result of the last execution of this function will directly or indirectly affect the results of this execution, such as arithmetic).
What are the similarities and differences between it and global variables?
Global variables are, by definition, global. Object-oriented programming is designed to achieve "high cohesion, low coupling". For example, the total population $count if you use global variables then the next time you hit the total, the total change is big; sometimes the code is probably not written by someone else if you use this variable, it is not a cup of ... But there's no problem if you use class static variables (at least each class has its own $count)

The constants in the class. What are the pros and cons of a constant defined in a class and a constant defined in a non-class?
A constant that is defined in a class differs from a constant defined in a non-class as the name implies the scope of use, such as gender
Person::gender_male = ' Male '
Person::gender_female = ' female '
Chicken::gender_male = ' Public '
Chicken::gender_female = ' mother '
Cat::gender_male = ' Male '
Cat::gender_female = ' female '
It is not so thoughtful to take care of defining these two constants in the global definition.

In what situations do you need to define constants?
such as the sex of human, the male and female of animals and so on. If you do not define a constant to use 0|1, male|female in case male wrong to become Mela that is not a cup of the tool?? And suppose someday you want to use 01 instead of male and female to indicate that you're searching for tens of thousands of lines of code. If the constant is not to change the definition of the place is good ...

#! /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;    The Public Function Setgender ($gender =self::gender_male) {$this->_gender = $gender; }} $ary = Array (); foreach (range (1, +) as $_) {$ary [] = new Person ("person$_");} echo Person:: $count, "\ n";      # # 100 Persons unset ($ary [15]); # # # destroys an echo Person:: $count, "\ n"; # # 99 Persons $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 ();

Use static variables to reduce the use of resources???
The difference between a static property of a class and a global variable is that the scope is different
When instantiating many objects from a class, a static property is a common data area, that is, the objects shared by each object.
This has nothing to do with the depletion of resources.

Similarly, class constants and constants are only different in scope
Similarly, the static method of a class is a set of functions with restricted scope

Since php5.3 re-introduced the concept of namespaces, those "static" gadgets will all be returned.

To learn object-oriented programming, we should look at the problem with object-oriented method
Don't be fooled by the very bad nouns of translation


Static variables self, and $this are really not very well understood.
The self can only be followed by a method, or a variable, but cannot be assigned a value!
I have an error in the following wording!

 
  Parse error:  PHPDocument26
$this->var2 = 1900;//The following error 2//echo Self::var2; Fatal error: PHPDocument28
Self::d UMP ();} function dump () {print_r ($this);}} $f = new father (); $f->init ();
  • 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.