The difference between self and static in php object-oriented programming matlab object-oriented programming c# object-oriented programming ideas object-oriented programming language

WBOY
Release: 2016-07-29 08:50:42
Original
1237 people have browsed it

In PHP object-oriented programming, you will always encounter

class test{
 public static function test(){
  self::func();

  static::func();
 }

 public static function func(){}
}

Copy after login

But do you know the difference between self and static?

In fact, the difference is very simple, you only need to write a few demos to understand:

Demo for self:

class Car
{
 public static function model(){
  self::getModel();
 }

 protected static function getModel(){
  echo "This is a car model";
 }
}

Copy after login

Car::model();

Class Taxi extends Car
{
 protected static function getModel(){
  echo "This is a Taxi model";
 }
}

Copy after login

Taxi::model();
get From the output

This is a car model
This is a car model
Copy after login

, we can see that self will still call the parent class’s method in the subclass. What is called is the method of the parent class, but the method called in the parent class method will also be the method of the subclass (so confusing...)

Before the PHP5.3 version, there was still a little difference between static and self. What is it specifically? , after all, it’s all version 7’s world. I won’t understand it anymore.

The summary is: self can only refer to methods in the current class, and the static keyword allows functions to dynamically bind methods in the class at runtime.

The above introduces the difference between self and static in PHP object-oriented programming, including object-oriented programming and static content. I hope it will be helpful to friends who are interested in PHP tutorials.

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!