Skip to content

Commit

Permalink
Change the attribute parent to Inherit, improved IDE friendliness
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 6, 2024
1 parent 904ff13 commit ad3ea79
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ configure.in
acinclude.m4
config.guess
config.sub
/composer.lock

17 changes: 12 additions & 5 deletions docs/cn/php/inherit.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ composer require swoole/phpy
```php
use phpy\PyClass;

#[parent('Animal', 'animal')]
#[Inherit('Animal', 'animal')]
class Dog extends PyClass
{
protected string $weight;
Expand Down Expand Up @@ -55,9 +55,9 @@ class Dog extends PyClass
```

- `PHP` 类必须要继承自 `PyClass` 基类
- 使用 `#[parent('Animal', 'animal')]` 属性声明继承关系
- 使用 `#[Inherit('Animal', 'animal')]` 属性声明继承关系
- 第一个参数为 `Python` 类名,第二个参数为 `Python` 包名
- 支持多继承,多个 `#[parent]` 属性声明
- 支持多继承,可添加多个 `#[Inherit]` 属性声明
- 在子类的构造方法必须执行父类的构造方法 `parent::__construct()`,否则会报错

## 调用基类构造方法
Expand Down Expand Up @@ -96,11 +96,18 @@ $this->super()->speak('dog');
## 多重继承

```php
#[parent('Animal', 'animal')]
#[parent('Base', 'dog')]
#[Inherit('Animal', 'animal')]
#[Inherit('Base', 'dog')]
class Dog extends PyClass {}
```

等同于如下的 `Python` 代码:
```python
class Dog(Animal, Base):
pass
```


## 传递对象到 `Python`
```php
$framework = PyCore::import('framework');
Expand Down
11 changes: 7 additions & 4 deletions examples/class/Dog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

use phpy\PyClass;


#[parent('Animal', 'animal')]
/**
* @property string $color
* @method get_age()
*/
#[Inherit('Animal', 'animal')]
class Dog extends PyClass
{
function __construct(string $name, int $age)
Expand All @@ -13,14 +16,14 @@ function __construct(string $name, int $age)
$this->super()->__init__($name, $age);
}

protected function test()
protected function test(): void
{
debug_print_backtrace();
}

function speak(string $name): void
{
echo "Dog $name, color: {$this->self()->color}, speak: wang wang wang\n";
echo "Dog $name, color: {$this->color}, speak: wang wang wang\n";
$this->super()->speak('dog');
}
}
13 changes: 13 additions & 0 deletions lib/phpy/Inherit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace phpy;

#[\Attribute]
class Inherit
{
public function __construct(string $parentClass, string $package = '')
{
}
}

class_alias(Inherit::class, \Inherit::class);
12 changes: 8 additions & 4 deletions lib/phpy/PyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ function __construct()
$this->_proxyClass = strtolower(str_replace('\\', '_', $class));
$this->_proxyFile = self::$_proxyPath . '/' . $this->_proxyClass . '.py';

if (self::$_checkStat) {
$hasProxyFile = is_file($this->_proxyFile);
if ($hasProxyFile and self::$_checkStat) {
$this->checkProxyFile();
}

if (!is_file($this->_proxyFile)) {
if (!$hasProxyFile) {
$this->makeProxy();
}

PyCore::import($this->_proxyClass)->{$this->_proxyClass}($this);
}

Expand All @@ -67,7 +68,10 @@ function __construct()
private function makeProxy(): void
{
$ref = new ReflectionClass($this);
$refParents = $ref->getAttributes('parent');
$refParents = $ref->getAttributes('inherit');
if (empty($refParents)) {
$refParents = $ref->getAttributes('parent');
}

$import = '';
$parents = [];
Expand Down

0 comments on commit ad3ea79

Please sign in to comment.