diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..4da761ae11d5ae075d462bf81f7ecc57e9a84674 --- /dev/null +++ b/README.en.md @@ -0,0 +1,82 @@ +# PHP Technical Points + +This project demonstrates several important PHP technical concepts, including the difference between `self` and `static`, the use of Traits, and the application of the Singleton pattern. + +## Project Structure + +``` +├── self_vs_static.php/ +│ └── main.php # Demonstrates the difference between self and static +├── trait/ +│ ├── card.php # Card Trait definition +│ ├── database.php # Database and Logger classes +│ ├── main.php # Order class +│ └── singleton.php # Singleton Trait +``` + +## Core Concepts + +### 1. self vs static + +Both `self` and `static` in PHP are used to reference properties and methods of the current class, but they have a key distinction: + +- **`self`**: Refers to the class in which the method/property is defined (statically bound) +- **`static`**: Refers to the class that is actually calling the method/property (dynamically resolved at runtime) + +```php +class Music { + public static function getInstanceBySelf() { + return new self(); // Always returns a Music instance + } + + public static function getInstanceByStatic() { + return new static(); // Returns an instance of the class that called the method + } +} + +class Song extends Music { + // ... +} +``` + +When calling `Song::getInstanceBySelf()`, a `Music` instance is returned; when calling `Song::getInstanceByStatic()`, a `Song` instance is returned. + +### 2. Trait + +A Trait is a mechanism in PHP for code reuse, allowing developers to declare groups of methods that can be reused in multiple classes. + +```php +trait Singleton { + private static $instance; + + public static function getInstance() { + if (!self::$instance) { + self::$instance = new static(); + } + return self::$instance; + } +} +``` + +### 3. Singleton Pattern + +By making the constructor private and providing a static method to obtain the single instance, this pattern ensures that only one instance of a class exists. + +## Class Descriptions + +| Class Name | Description | +|------------|-------------| +| `Music` | Base music class demonstrating static method binding | +| `Song` | Subclass of Music | +| `Card` | Trait providing card-related functionality | +| `Database` | Database connection class | +| `Logger` | Logging class | +| `Order` | Order class | + +## Requirements + +- PHP 5.4.0 or higher (Trait support required) + +## License + +This project is for learning and reference purposes only. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e4a12f4745e57929449c4a7e5637164d511f4f64 --- /dev/null +++ b/README.md @@ -0,0 +1,84 @@ + + +# PHP 技术要点 (PHP Technical Point) + +本项目展示了 PHP 中的几个重要技术概念,包括 `self` 与 `static` 的区别、Trait 的使用以及单例模式的应用。 + +## 项目结构 + +``` +├── self_vs_static.php/ +│ └── main.php # 演示 self 与 static 的区别 +├── trait/ +│ ├── card.php # Card Trait定义 +│ ├── database.php # Database 和 Logger 类 +│ ├── main.php # Order 类 +│ └── singleton.php # Singleton Trait +``` + +## 核心概念 + +### 1. self vs static + +`self` 和 `static` 在 PHP 中都用于引用当前类的属性和方法,但存在关键区别: + +- **`self`**: 指向定义该方法/属性的类(即写死的类) +- **`static`**: 指向实际调用该方法/属性的类(运行时动态确定) + +```php +class Music { + public static function getInstanceBySelf() { + return new self(); // 始终返回 Music 实例 + } + + public static function getInstanceByStatic() { + return new static(); // 返回调用该方法的类实例 + } +} + +class Song extends Music { + // ... +} +``` + +当调用 `Song::getInstanceBySelf()` 时,返回的是 `Music` 实例;而调用 `Song::getInstanceByStatic()` 时,返回的是 `Song` 实例。 + +### 2. Trait + +Trait 是 PHP 中实现代码复用的机制,允许开发者声明可在其他类中重用的方法组。 + +```php +trait Singleton { + private static $instance; + + public static function getInstance() { + if (!self::$instance) { + self::$instance = new static(); + } + return self::$instance; + } +} +``` + +### 3. 单例模式 + +通过私有化构造函数并提供静态方法获取唯一实例,确保类只有一个实例存在。 + +## 类说明 + +| 类名 | 说明 | +|------|------| +| `Music` | 基础音乐类,演示静态方法的绑定 | +| `Song` | 音乐子类,继承自 Music | +| `Card` | Trait,提供卡片相关功能 | +| `Database` | 数据库连接类 | +| `Logger` | 日志记录类 | +| `Order` | 订单类 | + +## 运行要求 + +- PHP 5.4.0 或更高版本(支持 Trait) + +## 许可证 + +本项目仅供学习和参考使用。 \ No newline at end of file