PHP Object-oriented detailed (c) _php instances

Source: Internet
Author: User
Object-oriented object concept is the core of object-oriented technology. The things we face in the show world are objects, such as computers, televisions, bicycles, etc. In object-oriented program design, object is a whole that consists of information and the description of information processing, and it is an abstraction to the real world.

Main three attributes of an object

Object behavior: You can apply those actions to objects, turn on the lights, turn off the lights is the behavior.
The shape of the object: When applying those methods is how the object responds, color, size, shape.
Object representation: The representation of an object is equivalent to an identity card, specifically distinguishing between the same behavior and the state of what is different.

Object-oriented model

Object-oriented concepts:

OOP (object-oriented programming) it could be that its code is simpler, easier to maintain and more robust.

1, PHP Object-oriented (c)

Iv. advanced practices in OOP

4.3 static-static Member

<?phpdate_default_timezone_set ("PRC");/** * 1. The definition of a class begins with the Class keyword followed by the name of the class. The name of a class is typically capitalized in the first letter of each word. * 2. Defines the properties of the class * 3. Defines the method of the class * 4. Instantiate the Class object * 5. Use the properties and methods of the object */class human{public $name, public $height, public $weight;p ublic function Eat ($food) {echo $this->name. " S eating ". $food."
"; }} Class animal{public $kind, public $gender;} Class Nbaplayer extends human{//class's properties are defined public $name = "Jordan";//define Property public $height = "198cm", Public $weight = "98kg"; publ IC $team = "Bull"; Public $playerNumber = "23"; Private $age = "44"; Public $president = "David Stern"; The definition of a method of a 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 inside PHP, which means the object itself $this->height = $height; By $this you can set the property value of an object $this->weight = $weight; $this->team = $team; $this->playernumber = $playerNumber; }}/** * 1. When a class is instantiated as an object, the New keyword is used, followed by the name of the class and a pair of parentheses. * 2. You can use an object to perform an assignment like a different value */$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "Max"), echo "
"; $james =new Nbaplayer (" James "," 203cm "," 120kg "," Heat "," 6 "); echo"
The syntax used for accessing an object's properties is a symbol, followed by the name of the property echo $jordan->name. "
";//The syntax used by a method of a calling object is a notation, followed by the name of the method and a pair of parentheses $jordan->run (), $jordan->pass ();//The subclass calls the method of the parent class $jordan->eat (" Apple "), or//try calling private, directly and through the internal public function//$jordan->age; $jordan->getage (); echo"
"; $jordan->changepresident (" Adam Silver "); Echo $jordan->president."
"Echo $james->president."
"; Just start with the above example. What is wanted here is to change one of the two-bit objects at the same time. --with staticpublic static $president = "David Stern"; The definition of a method of a class public static function Changepresident ($newP) {static:: $president = $newP;//Here static change to self is more conforming to specification}

Note here the static position, as well, within the method::

The method of invocation is also changed.

echo Nbaplayer:: $president; echo "
"; Nbaplayer::changepresident ("Adam Silver"); Echo Nbaplayer:: $president; echo "
";

As stated before, static members are constants, so they are not targeted at a specific object (not constrained by specific objects)--based on this, the definition & assignment & call does not require specific object participation.

Internal call to use self/static::$ ...

External invocation, Class name::

Usefulness is the data shared by all objects.

--if internally called, the variable is in the parent class

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

public static $AAA = "DAFDFA";

Then in subclass Nbaplayer, when you call a static member of the parent class, you

Echo Parent:: $AAA;

And the external call, as stated above, the class name::, so, the direct parent class name can be

echo Human:: $AAA;

--Other

In a static method, you cannot access other variables, that is, you cannot use $this->

--Summary

/**
* Static members
* 1. Static properties are used to hold the class's public data
* 2. Static methods can only access static properties
* 3. Static members do not need to instantiate objects to access
* 4. Inside a class, you can access its own static members through the self or the static keyword
* 5. Static members of the parent class can be accessed via the parent keyword
* 6. Static members of a class can be accessed externally through the class name
*/

4.4 Final Members

--Questions

Do not want a class to have subclasses;

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

--final

"=PHP5 version

As an 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 ();

Subclasses write a method name that is exactly the same as the parent class (the content can be different), and the parent class method is rewritten!

So, do not want to be overridden by the method in the parent class, write the final

Copy the Code code as follows:
Final public Function test () {

And so on, for a parent class that does not want a subclass, write final in the class name

Copy the Code code as follows:
Final class baseclass{

--Summary

/**
* Rewrite and final
* 1. Subclasses write a method that is exactly the same as the parent class to complete the override of the parent class method
* 2. For classes that do not want to be inherited by any class, you can add the final keyword before class
* 3. For methods that do not want to override (overwrite, modify) the quilt class, you can add the final keyword before the method definition
*/

4.5 Data access

Get rid of final all first.

--parent

And then write in the method in the subclass

Parent::test ();

After running, you will still be able to invoke the parent's keyword, even if the overridden data

--self

Then write in the method test in the parent class

Self::test1 ();

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

--Summary

/**
* Data Access Supplement
* 1. The parent keyword can be used to invoke class members that are overridden by the parents class
* 2. The Self keyword can be used to access a member method of the class itself, or to access its own static members and class constants, not to access the properties of the class itself, and to access the class constants without adding a $ symbol in front of the constant name
* 3. The static keyword is used to access statically defined members of the class, and it is necessary to add the $ symbol before the property name when accessing the static property
*/

4.6 Object Interface

Very IMPORTANT!!!

--Questions

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

For example, people and animals eat things, but the way they eat is not the same.

--Definition

Interface is to define the common behavior of different classes, and then implement different functions within different classes.

--Chestnut

Copy the Code code as follows:
Define an interface
Interface icaneat{
Public function eat ($food);
}

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

So, here's the "human will eat"

The specific object, connected to the interface class Human implements Icaneat{public function Eat ($food) {echo "Human eating". $food.
"; }} $obj =new Human (), $obj->eat ("shit");

Please ignore the "food" I gave.

Note that extends is no longer used, but implements. Then, the same method name is exactly the same. Then, the object must/best implement the method.

Go on

Interface icaneat{public Function eat ($food);} The 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 them too!

--Reverse operation

Determines whether an object is connected to an interface.

Copy the Code code as follows:
Var_dump ($obj instanceof icaneat);

A Boolean value is returned.

--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 demo N eat ". $food;}} $ghost =new Demon (); $ghost->pee (); $ghost->eat ("shit");

Interfaces are also essentially classes, which can be inherited/inherited, but interfaces that use the inherited interface are implemented specifically for all parent, "master" methods.

--Summary

/**
* Interface
* 1. Basic concepts and basic usage of interfaces
* 2. There is no specific implementation of the method in the interface
* 3. A class that implements an interface must provide a method defined in the interface
* 4. An object cannot be created with an interface, but it is possible to determine whether an object implements an interface
* 5. Interfaces can inherit interfaces (interface extends interface)
* 6. All methods defined in the interface must be public, which is the attribute of the interface.
*/

Aaaaaaaaaaaaaa

Bu Xiang xie le ......... .....

Ming Tian Yao ge ..... .....

The above content is small to introduce you to the PHP object-oriented detailed (three), I hope you like.

  • Related Article

    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.