Table of Contents
Desolate Saturday-PHP object-oriented (3), Saturday-php object-oriented
Home Backend Development PHP Tutorial Deserted Saturday - PHP object-oriented (3), Saturday - PHP object-oriented_PHP tutorial

Deserted Saturday - PHP object-oriented (3), Saturday - PHP object-oriented_PHP tutorial

Jul 12, 2016 am 09:03 AM

Desolate Saturday-PHP object-oriented (3), Saturday-php object-oriented

 hi 

It’s Kaizen Saturday again. The clothes I had accumulated for two weeks were finally almost finished. Come here to learn something in the afternoon~~

1. PHP object-oriented (3)

4. Advanced Practice of OOP

4.3 Static-static members

date_default_timezone_set("PRC");
/**
* 1. The definition of a class starts with the class keyword, followed by the name of the class. Class names are usually named with the first letter of each word capitalized.
* 2. Define the attributes of the class
* 3. Define the methods of the class
* 4. Instantiate the object of the class
* 5. Use the attributes and methods of the object
*/

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{
// Definition of class attributes
public $name="Jordan"; // Define attributes
public $height="198cm";
public $weight="98kg";
public $team="Bull";
public $playerNumber="23";
private $age="44";
public $president="David Stern";



// Definition of class method
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 pass 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, representing the object itself
$this->height = $height; // $this can be used to set the attribute value of the object
$this->weight = $weight;
$this->team = $team;
$this->playerNumber = $playerNumber;
}

}


/**
* 1. Use the new keyword when instantiating a class into an object, followed by new followed by the name of the class and a pair of parentheses.
* 2. Using objects can perform assignment operations just like using other values ​​
*/
$jordan = new NbaPlayer("Jordan", "198cm","98kg","Bull","23");echo "
";
$james=new NbaPlayer("James", "203cm", "120kg", "Heat", "6");echo "
";
// Visit The syntax used for the properties of an object is the -> symbol, followed by the name of the property
echo $jordan->name."
";
// Used to call a method of the object The syntax is the -> symbol, followed by the name of the method and a pair of brackets
$jordan->run();
$jordan->pass();
//The subclass calls the parent class The 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."< br/>";

Let’s start directly from the above example.

What you want to achieve here is, change a certain variable of the two objects at the same time. ——Use static

public static $president="David Stern";

// Definition of class method
public static function changePresident($newP){
static::$president=$newP;//Replacing static with self here is more in line with the specification
}

Pay attention to the position of static here, and the ::

in the method

The calling method has also changed.

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

That is, as mentioned before, a static member is a constant, so it is not targeted at a specific object (not restricted by a specific object) - Based on this, definition, assignment, and call are not Requires specific target participation.

For internal calls, use self/static::$...

External call, class name::

The purpose is data shared by all objects.

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

For example, in the above example, write this sentence in the parent class human

public static $aaa="dafdfa";

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

echo parent::$aaa;

For external calls, as mentioned above, class name::, so just use the parent class name directly

echo Human::$aaa;

--Others

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

--Summary

/**
* Static members
* 1. Static properties are used to save the public data of the class
* 2. Only static properties can be accessed in static methods
* 3. Static members do not need to instantiate objects. Access
* 4. Within a class, you can access its own static members through the self or static keyword
* 5. You can access the static members of the parent class through the parent keyword
* 6. You can access it through the class name External access to static members of a class
*/

4.4 Final members

--Question

Don’t want a class to have subclasses;

Don’t want the subclass to modify a variable in the parent class (avoid overriding?)

--final

》=php5 version

Give me 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();

Writing the same method name in the subclass as in the parent class (the content can be different) will complete the rewriting of the parent class method!

Therefore, for methods in the parent class that you do not want to be overridden, write final

final public function test(){

And so on, for a parent class that does not want to have subclasses, write final

in the class name

final class BaseClass{

--Summary

/**
* Rewriting and Final
* 1. Writing a method in the subclass that is exactly the same as the parent class can complete the rewriting 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 be overwritten (overwrite, modified) by subclasses, you can add the final keyword
in front of the method definition.*/

4.5 Data Access

Remove the final first

--parent

Then write

in the method in the subclass

parent::test();

After running, you will find that you can still call the parent class through the parent keyword, even if it is rewritten data

--self

Then write

in the method test in the parent class

self::test1();

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

--Summary

/**
* Data access supplement
* 1. The parent keyword can be used to call the overridden class members of the parent class
* 2. The self keyword can be used to access the member methods of the class itself, or it can be used It is used to access its own static members and class constants; it cannot be used to access the properties of the class itself; when accessing class constants, there is no need to add the $ symbol in front of the constant name
* 3. The static keyword is used to access static members defined by the class itself. When accessing static properties, you need to add the $ symbol
in front of the property name.*/

4.6 Object Interface

Very important! ! !

--Question

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

For example, both humans and animals eat, but the ways of eating are different.

--Definition

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);
}

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

So, here it is, "Humans can 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");

Please ignore the "food" I gave.

Note that does not use extends anymore, but implements. Then, the same method name is exactly the same. Then the object must/preferably implement the method.

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 too!

--Reverse operation

Determine whether an object is connected to an interface.

var_dump($obj instanceof ICanEat);

will return 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");

Interfaces are essentially classes and can be inherited/inherited. However, when using an inherited interface, all methods of the parent class and "grandfather class" must have specific implementations.

--Summary

/**
* Interface
* 1. The basic concepts and basic usage of interfaces
* 2. The methods in the interface have no specific implementation
* 3. The class that implements an interface must provide the interface Defined methods
* 4. You cannot use interfaces to create objects, but you can determine whether an object implements an interface
* 5. Interfaces can inherit interfaces (interface extends interface)
* 6. In interfaces All methods defined must be public, which is a characteristic of interfaces.
*/

aaaaaaaaaaaaaa

bu xiang xie le......................

ming tian yao ge............

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1077971.htmlTechArticleDesolate Saturday-PHP object-oriented (3), Saturday-php object-oriented hi Kaisen again It's Saturday. The clothes I had accumulated for two weeks were finally almost finished. I just came here to learn something in the afternoon...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles