


My thoughts are messy. It turns out that non-static methods can also be called statically (transferred). My thoughts are static_PHP tutorial
My thoughts are messy. It turns out that non-static methods can also be called statically (transferred). My thoughts are static
1. Non-static methods can be called statically in PHP Static method?
Today I was asked whether I can use the className::methodName() method in PHP to call a method that is not declared Static. In my mind, I seem to have seen this usage before, but I'm a little unsure. As we all know, in manuals or tutorials, methods are divided into static methods and non-static methods. Usually the methods we call statically must be static methods.
What happens if we call a non-static method? Do a test first.<?php<code><?php<br>
class test{<br>
function test(){<br>
echo 'it works';<br>
}<br>
}<br>
test::test();
class test{
function test(){
echo 'it works';
}
}Fatal error:
Non-static method test::test() cannot be called statically in
/home/×××/test.php<br>
on line 7 Call Stack: 0.0002 332548 1. {main}()
/home/×××/test.php:0
test::test();
Execute the following and the error returned is as follows
Fatal error:
Non-static method test::test() cannot be called statically in
/home/×××/test.php<span>
on line 7 Call Stack: 0.0002 332548 1. {main}()
/home/×××/test.php:0</span>
At this time, you may think that calling non-static methods statically is not feasible, but in fact, it is too early to draw a conclusion, because the test() method is quite special. It has the same name as the class and is a constructor method.
<?php<br>
class test{<br>
function test(){<br>
echo 'it works';<br>
}<br>
function test2(){<br>
echo 'it works too';<br>
}<br>
}<br>
test::test2();
Let’s continue testing. it works too
<?php<strong>
class test{</strong>
function test(){<br>
echo 'it works';
}
function test2(){<?php<br>
class test{<br>
static function test(){<br>
echo 'it works';<br>
}<br>
}<br>
test::test();
echo 'it works too';
}
}Fatal error: Constructor test::test() cannot be static in
/home/xxx/test.php on line 9
test::test2();
Execution result:
This shows that it is possible to statically call non-static methods, but it is not possible to statically call constructors . In order to verify this conclusion, I did the following test:
Cannot make a static reference to the non-static method
showString() from the type HelloWorldApp
<?php<br>
class test{
static function test(){
} } test::test(); The execution result is as follows: Constructors cannot be declared static, so the above inference is correct.
But this result is indeed very special, because maybeonly PHP can statically call non-static methods. I did an experiment with Java. If you statically call a non-static method, the following error will be reported:
I have not tried other languages one by one, but this is enough to illustrate the special features of PHP. I have not found any relevant explanation as to why PHP has such a situation.
<?php<br>
class test{function test2(){}}
for($k=0; $k<10000; $k )
{
test::test2();
}
2. Should static calls to non-static methods be applied? <?php<br>
class test{static function test2(){}}
for($k=0; $k<10000; $k )
{
test::test2();
}
Can we use this method instead of the static method? First of all, from the perspective of code readability, statically calling non-static methods is of course not recommended
<?php
class test{static function test2(){}}
for($k=0; $k<10000; $k )<div class="art_confoot">
{</div>
test::test2();
}
The above code execution time is between 5 and 10 milliseconds. From this point of view, the efficiency of statically calling non-static methods is much lower than standard static method calls, so statically calling non-static methods is not recommended in terms of efficiency. .
http://www.bkjia.com/PHPjc/1135471.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1135471.htmlTechArticleMy thoughts are messy. It turns out that non-static methods can also be statically called (transferred). My thoughts are static 1. Static methods can be used in PHP Call non-static methods? Today I was asked if I can use className:... in PHP

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

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
