Barren Saturday-php Object-oriented (iii), Saturday-php Object-oriented _php tutorial

Source: Internet
Author: User

Barren Saturday-php Object-oriented (iii), Saturday-php Object-oriented


Hi

It's the Saturday of Kai Sen again. The two-week-long clothes that were saved were almost finished. The big afternoon to learn something ~ ~

1, PHP Object-oriented (c)

Iv. advanced practices in OOP

4.3 static-static Member

Date_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. Defining the properties of a class
* 3. Methods for defining classes
* 4. Instantiating an object of a class
* 5. Working with properties and methods of objects
*/

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's properties are defined
public $name = "Jordan";//define Properties
Public $height = "198cm";
Pu Blic $weight = "98kg";
Public $team = "Bull";
Public $playerNumber = "23";
Private $age = "44";
Public $president = "David Stern";



//Definition of method for 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 that represents the object itself
$this->height = $height;//$this can set the object's property value
$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. Use an object to perform assignment as you would with other values
*/
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "+"), 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 the calling object is a symbol, followed by the name of the method and a pair of parentheses
$jordan->run ();
$jordan->pass ();
Subclasses call methods of parent class
$jordan->eat ("Apple");
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 Static

public static $president = "David Stern";

Definition of a method of a class
Public static function changepresident ($newP) {
Static:: $president = $newP;//Here static change to self more conform 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

final Public Function test () {

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

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

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"

specific objects, connecting to interfaces
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);
}

specific objects, connecting to interfaces
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.

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

http://www.bkjia.com/PHPjc/1077971.html www.bkjia.com true http://www.bkjia.com/PHPjc/1077971.html techarticle Barren Saturday-php Object-oriented (iii), Saturday-php Object-oriented hi is also the Saturday of Kai Sen. The two-week-long clothes that were saved were almost finished. The big afternoon to learn some East ...

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