PHP object-oriented explanation (iii) _ php instance

Source: Internet
Author: User
This article mainly introduces PHP object-oriented explanation (3). If you need it, you can refer to the object-oriented concept, which is the core of object-oriented technology. In the display world, what we face is objects, such as computers, televisions, and bicycles. In object-oriented programming, an object is a whole composed of information and descriptions of information processing. It is an abstraction of the real world.

Three main features of an object

Object behavior: You can perform operations on the object. If you turn on the light, turning off the light is behavior.
Object form: How the object responds, colors, dimensions, and shapes are applied.
Object Representation: the representation of an object is equivalent to an ID card, which specifically distinguishes between the same behavior and status.

Object-Oriented model

Object-oriented concepts:

Oop (Object-Oriented Programming) can make its code simpler and easier to maintain and has stronger reusability.

1. PHP object-oriented (3)

Iv. advanced OOP practices

4.3 Static-Static member

<? Phpdate_default_timezone_set ("PRC");/*** 1. 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 class attributes * 3. method for defining classes * 4. instantiation 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 {// class attribute definition public $ name =" Jordan "; // define the attribute public $ height = "198"; public $ weight = "98 kg"; public $ team = "Bull"; public $ playerNumber = "23 "; private $ age = "44"; public $ president = "David Stern"; // class method definition 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 property value of an object through $ this-> weight = $ weight; $ this-> team = $ team; $ this-> playerNumber = $ playerNumbe R ;}}/*** 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 an object just like other values */$ jordan = new NbaPlayer ("Jordan", "198 cm", "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 the object is-> symbol, followed by the method name and a pair of brackets $ jordan-> run (); $ jordan-> pass (); // subclass calls the parent class's method $ jordan-> eat ("apple"); // try to call private, directly and through the internal public function // $ jordan-> age; $ jordan-> getAge (); echo"
"; $ Jordan-> changePresident (" Adam Silver "); echo $ jordan-> president ."
"; Echo $ james-> president ."
"; Let's start with the above example. What we want to get here is to change a variable of two objects at the same time. -- Use staticpublic static $ president = "David Stern"; // class method to define public static function 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.

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

As previously mentioned, static members are constants, so they are not targeted at a specific object (not restricted by a specific object)-based on this, no specific object is required for definition, 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

Echo parent: $ 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 the name of the parent class in the subclass (the content can be different). This will overwrite the parent class method!

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

The Code is as follows:


Final public function test (){


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

The Code is as follows:


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 that you can still use 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 that self 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

The Code is as follows:


// 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 that extends is not used, but 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 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.

The Code is as follows:


Var_dump ($ obj instanceof ICanEat );


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 ..............

The above content is a small series of PHP object-oriented explanation (3), I hope you like it.

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.