


What are the differences between static calls and instantiated calls of PHP classes?
The differences are: 1. Static methods generate memory when the program starts, and instance methods generate memory while the program is running; 2. Static methods can be called directly, and instance methods must first generate an instance, and then call the method through the instance. ; 3. Static memory is continuous, and instances apply for discrete space, so it is not as fast as static methods, and static memory is limited.
There is only one static method. No matter how many times you call it, it is shared. There is no concept of object, so it cannot be used statically. Use $this to call in the method. If you have to call, you can only instantiate your own class
Instancing is different. Each instantiation is an object and has multiple.
Let’s use an example to understand
<?php Error_reporting(E_ALL|E_STRICT); class A{ public function bar(){ echo 'bar'.PHP_EOL; } public static function foo(){ echo 'foo'.PHP_EOL; } } A::bar();//会报错 A::foo();//正确 $obj = new A(); $obj -> foo();//正确 /* bar()是一个非静态方法,应该由对象来调用,但用静态来调用此方法也可以执行,而严格状态下,此方法会执行,同时报错, Strict Standards: Non-static method Human::easyeat() should not be called statically in........ 但是实例化的类是可以调用静态方法的。 */
<?php class A{ public $name = 'kangbazi'; public function bar(){ echo 'bar'.PHP_EOL; } public static function foo(){ echo self::$name.PHP_EOL;//报错 } } A::foo(); /* 静态方法不能调用非静态属性 。不能使用self::调用非静态属性。 */
Make a short summary:
The biggest difference is memory.
Static methods generate memory when the program starts, instance methods generate memory while the program is running,
So static methods can be called directly, instances The method must first generate an instance and call the method through the instance. The static speed is very fast, but if it is too much, it will occupy memory.
Any language operates on memory and disk. As for whether it is object-oriented, it is just a question of the software layer. The bottom layer is the same, but the implementation method is different.
Static memory is continuous because it is generated at the beginning of the program, and the instance applies for discrete space, so of course it is not as fast as the static method,
And static memory is limited, if there is too much, the program will not be able to start.
Recommended tutorial: "php tutorial"
The above is the detailed content of What are the differences between static calls and instantiated calls of PHP classes?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.