Saturday-PHP object-oriented (3)

Source: Internet
Author: User
Tags object oriented php
The ridiculous Saturday-object oriented PHP (3) hi is Kesson's Saturday. The accumulated clothes for two weeks are almost finished. I will learn something later in the afternoon ~~ 1. PHP object-oriented (III) IV. OOP advanced practice 4.3Static-static member & lt ;? Phpdate_default_timezone_set (PRC); *** 1. class definition starts with the class keyword, followed by the class name. A ridiculous Saturday-PHP object-oriented (III)

Hi

It's another Saturday in Kesson. The accumulated clothes for two weeks are almost finished. I will learn something later in the afternoon ~~

1. PHP object-oriented (3)

IV. advanced OOP practices

4.3 Static-Static member

Date_default_timezone_set ("PRC ");
/**
* 1. the class definition starts with the class keyword, followed by the class name. The class name is usually the first letter of each word in uppercase.
* 2. define the attributes of a class
* 3. define class methods
* 4. instantiate the class object
* 5. use object attributes and methods
*/

Class Human {
Public $ name;
Public $ height;
Public $ weight;

Public function eat ($ food ){
Echo $ this-> name. "'s eating". $ food ."
";
}
}

Class Animal {
Public $ kind;
Public $ gender;
}


Class NbaPlayer extends Human {
// Define the attributes of a class
Public $ name = "Jordan"; // define attributes
Public $ height = "198 ";
Public $ weight = "98 kg ";
Public $ team = "Bull ";
Public $ playerNumber = "23 ";
Private $ age = "44 ";
Public $ president = "David Stern ";



// Define the method of the class
Public function changePresident ($ newP ){
$ This-> president = $ newP;
}

Public function run (){
Echo "Running
";
}

Public function jump (){
Echo "Jumping
";
}

Public function dribble (){
Echo "Dribbling
";
}

Public function shoot (){
Echo "Shooting
";
}

Public function dunk (){
Echo "Dunking
";
}

Public function pass (){
Echo "Passing
";
}

Public function getAge (){
Echo $ this-> name. "'s age is". $ this-> age;
}

Function _ construct ($ name, $ height, $ weight, $ team, $ playerNumber ){
Print $ name. ";". $ height. ";". $ weight. ";". $ team. ";". $ playerNumber. "\ n ";
$ This-> name = $ name; // $ this is a pseudo variable in php, indicating the object itself.
$ This-> height = $ height; // you can set the attribute value of an object through $ this.
$ This-> weight = $ weight;
$ This-> team = $ team;
$ This-> playerNumber = $ playerNumber;
}

}


/**
* 1. when the class is instantiated as an object, the new keyword is used, followed by the class name and a pair of parentheses.
* 2. you can assign values to objects just like other values.
*/
$ Jordan = new NbaPlayer ("Jordan", "198", "98 kg", "Bull", "23"); echo"
";
$ James = new NbaPlayer ("James", "203", "120", "Heat", "6"); echo"
";
// The syntax used to access the object's properties is-> symbol, followed by the attribute name
Echo $ jordan-> name ."
";
// The syntax used to call a method of an object is-> symbol, followed by the method name and a pair of parentheses
$ Jordan-> run ();
$ Jordan-> pass ();
// Subclass calls the method of the parent class
$ Jordan-> eat ("apple ");
// Try to call private directly and through internal public functions
// $ Jordan-> age;
$ Jordan-> getAge (); echo"
";

$ Jordan-> changePresident ("Adam Silver ");
Echo $ jordan-> president ."
";
Echo $ james-> president ."
";

Start with the above example.

What you want here is,Change a variable of two objects at the same time. -- Use static

Public static $ president = "David Stern ";

// Define the method of the class
PublicStaticFunction changePresident ($ newP ){
Static: $ president = $ newP; // Here, static is changed to self, which is more compliant.
}

Note the static position here and the following in the method ::

The called method also changes.

EchoNbaPlayer: $ president;Echo"
";
NbaPlayer: changePresident ("Adam Silver ");
Echo NbaPlayer: $ president; echo"
";

That is, as mentioned earlier,A static member is a constant, so it does not target a specific object (not restricted by a specific object)-- Based on this, no specific object is required for definition, value assignment, or call.

Use self/static ::$ for internal calls...

External call, class name ::

It is the data shared by all objects.

-- If the variable is in the parent class during internal calling

For example, in the above example, the parent class human writes such a sentence

Public static $ aaa = "dafdfa ";

Then, in the subclass nbaplayer, when calling the static member of the parent class

EchoParent: $ aaa;

For external calls, as mentioned above, the class name is:, so you can directly use the parent class name.

Echo Human: $ aaa;

-- Others

In static methods, other variables cannot be accessed, that is, $ this-> cannot be used.

-- Summary

/**
* Static members
* 1. static attributes are used to save the public data of the class.
* 2. only static attributes can be accessed in static methods.
* 3. static members can be accessed without instantiating objects
* 4. inside the class, you can use the self or static keyword to access its own static members.
* 5. You can use the parent keyword to access static members of the parent class.
* 6. you can use the class name to access static members of the external worker class.
*/

4.4 Final Member

-- Problem

You do not want a class to have a subclass;

Do not want subclass to modify a variable in the parent class (avoid rewriting ?)

-- Final

"= Php5 version

For example

Class BaseClass {
Public function test (){
Echo "BaseClass: test called
";
}

Public function test1 (){
Echo "BaseClass: test1 called
";
}
}

Class ChildClass extends BaseClass {
Public function test (){
Echo "ChildClass: test called
";
}
}

$ Obj = new ChildClass ();
$ Obj-> test ();

Compile a method name that is exactly the same as that in the parent class in the subclass (the content can be different ).Rewrite!

Therefore, if you do not want to override the methods in the parent class, write final

FinalPublic function test (){

For parent classes that do not want subclass, write final in the class name.

Final class BaseClass {

-- Summary

/**
* Rewrite and Final
* 1. compile a method that is exactly the same as the parent class in the subclass to rewrite the parent class method.
* 2. for classes that do not want to be inherited by any class, you can add the final keyword before the class.
* 3. for methods that do not want to overwrite (Modify) the quilt class, you can add the final keyword before the method definition.
*/

4.5 data access

Remove final First

-- Parent

Then write it in the method in the subclass.

Parent: test ();

After running, you will find thatUse the parent keyword to call the parent class, even the overwritten data.

-- Self

Then write it in the method test of the parent class.

Self: test1 ();

After running, it is found thatSelf can call data in the same class (other methods/static variables/constant const)

-- Summary

/**
* Data access supplement
* 1. the parent keyword can be used to call a class member whose parent class is overwritten.
* 2. the self keyword can be used for member methods of the internal class, or for accessing its own static members and class constants. it cannot be used for attributes of the internal class. when a constant of the internal class, you do not need to add the $ symbol before the constant name.
* 3. the static keyword is used for static members defined by the category class. to access static attributes, you must add the $ symbol before the attribute name.
*/

4.6 object interface

Very important !!!

-- Problem

Different classes have the same behavior, but the same behavior has different implementation methods.

For example, humans and animals both eat, but the way they eat is not the same.

-- Definition

The interface defines the common behaviors of different classes and then implements different functions in different classes.

-- Chestnut

// Define an interface
Interface ICanEat {
Public function eat ($ food );
}

We can see that there is no specific implementation of the method in the interface, but there must be a method!

Then, the following is: "Humans will eat"

// Specific object, connected to the interface
Class Human implements ICanEat {

Public function eat ($ food ){
Echo "Human eating". $ food .".
";
}
}

$ Obj = new Human ();
$ Obj-> eat ("shit ");

Ignore the "food" that I give ".

Note,Instead of using extends, implements. Then, the method names are identical. Then, the object must/is best implemented.

Continue

Interface ICanEat {
Public function eat ($ food );
}

// Specific object, connected to the interface
Class Human implements ICanEat {
Public function eat ($ food ){
Echo "Human eating". $ food .".
";
}
}

Class Animal implements ICanEat {
Public function eat ($ food ){
Echo "Animal eating". $ food .".
";
}
}


$ Obj = new Human ();
$ Obj-> eat ("shit ");

$ Monkey = new Animal ();
$ Monkey-> eat ("banana ");

Let the animals eat it too!

-- Reverse operation

Determines whether an object is connected to an interface.

Var_dump ($ objInstanceofICanEat );

Returns a boolean value.

-- More chestnuts

Interface ICanPee extends ICanEat {
Public function pee ();
}

Class Demon implements ICanPee {
Public function pee (){
Echo "Can demon pee? ";
}
Public function eat ($ food ){
Echo "Can demon eat". $ food;
}
}

$ Ghost = new Demon ();
$ Ghost-> pee ();
$ Ghost-> eat ("shit ");

An interface is essentially a class that can be inherited or inherited. however, to use an inherited interface, all methods of the parent class and "parent class" must be implemented.

-- Summary

/**
* Interface
* 1. Basic concepts and usage of interfaces
* 2. there is no specific implementation of the methods in the interface.
* 3. classes that implement an interface must provide methods defined in the interface
* 4. you cannot use interfaces to create objects. However, you can determine whether an object has implemented an interface.
* 5. interfaces can inherit interfaces (interfaces extends interfaces)
* 6. all methods defined in the interface must be public, which is a feature of the interface.
*/

Aaaaaaaaaaaaaa

Bu xiang xie le ....................

Ming tian yao ge ..............

Rickon on 1 floor
When I came to visit the landlord, the landlord had perseverance. I couldn't even stick to running for a week. what do you do?
Re: tough recovery
@ Rickon: there may not be too many distractions. I don't play any other games without my girlfriend or even close my circle of friends (because it is too trivial and troublesome )., I usually go to the teaching and research section during the day, and study at night, and sleep on weekends ., Then, the two key points may be: one is to look for a job next year, and the other is to learn something better; the other is to follow the progress on the internet. if you learn it yourself, it is too difficult to start, this is better than self-control ., If your persistence persists, I think you can find something to make positive feedback. Simply put, every day you stick to it, you get something, but you need to visualize it, just like playing a game and upgrading it for a while, intuitive digital things have a big impact ., Hope to help you.

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.