- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-best-practices/prefer-static-reference
Language: PHP
Severity: Warning
Category: Best Practices
This rule relates to how static methods are referenced within a class in PHP. The self
keyword is used to refer to the same class in which the new keyword is being used. However, when dealing with static methods, it’s considered best practice to use the static
keyword instead of self
.
The reason for this preference lies in the late static binding concept of PHP. If a child class inherits a static method from a parent class and you use self
to reference it, it will call the parent’s version of the method. But if you use static
, it will call the child’s version if it exists, providing more flexibility and correctly implementing the behavior of inheritance in object-oriented programming.
To adhere to this rule, always use static
instead of self
when referencing static methods within the same class. For instance, instead of self::print()
, use static::print()
. This practice ensures that your code is more robust and adaptable to changes in class hierarchy.
<?php
public class Test {
private static function print() {
echo "Testing";
}
public static function display() {
self::print();
}
}
<?php
public class Test {
private static function print() {
echo "Testing";
}
public static function display() {
static::print();
}
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products