マジックメソッド__callStaticにおけるTrait関数のエイリアス定義
P粉226642568
P粉226642568 2023-07-31 11:46:23
0
1
364
<p>__callStatic メソッドを使用して Builder の新しいインスタンスを作成し、存在する場合は呼び出された関数を実行する Queryable 特性があります。クエリを連鎖させるための Builder を返します (Eloquent と同様)。 <br /><br />今、find() 関数を必要とするインターフェイスを実装しようとしていますが、find() 関数は Queryable トレイト (@method static static find) の phpDoc で宣言されています。 (mixed $primaryKeyValue) )、競合が発生します: </p><p><code></code></p> <ブロック引用> <p>Queryable の find(primaryKeyValue:mixed) の宣言は、StorageInterface->find(primaryKeyValue:mixed) と互換性がある必要があり、インターフェイス メソッドを作成できません。 </p> </blockquote> <p>この競合を解決するために、トレイト エイリアスと同様のメソッドを使用しようとしました (Queryable { Queryable::find as queryFind; } を使用します)。しかし、その後、次のような問題が発生しました。</p> <ブロック引用> <p>コンパイル エラー: appFrameworkTraitsQueryable::find にエイリアスが定義されていますが、メソッドが存在しません。</p> </blockquote> <p>解決策が見つからないようです。誰か助けてくれませんか? :) (これは魔法のメソッドなので、これはクラスの存在チェックに関係していると思いますが、修正方法もわかりません。) </p> <pre class="brush:php;toolbar:false;"><?php 名前空間 appFrameworkTraits; appFrameworkDatabaseBuilder を使用します。 appFrameworkDatabasePaginator を使用します。 /*** @method static int count() * @method static static|array最初(int $amount = 1) * @method static static|array最後(int $amount = 1) * @メソッド静的文字列getQuery() * @メソッド静的文字列getRawQuery() * @メソッド静的文字列getPrimaryKey() * @method static static find(mixed $primaryKeyValue) * @method 静的配列全て() * @method 静的配列得る() * @method 静的ページネーター paginate() * @method 静的ビルダー テーブル(文字列 $tableName) * @メソッド静的ビルダーprimaryKey(string $primaryKey) * @method 静的ビルダー perPage(int $perPageAmount) * @method static Builder where(string $column、mixed $operatorOrValue = null、mixed $value = null) * @method static Builder whereIn(string $column, array $search) * @method 静的ビルダー whereNotNull(string $column) * @method static Builder select(...$columns) * @method static Builder with(string $relation, Closure $closure) * @method static Builder orderBy(string $column, string $direction = 'desc') * @method 静的ビルダー制限(int $amount) * @method 静的ビルダーオフセット(int $amount)*/ 特性クエリ可能 { パブリック静的関数 __callStatic(string $name, array $arguments) { $functions = get_class_methods(Builder::class); if (in_array($name, $functions)) { $builder = 新しいビルダー(static::class); return $builder->$name(...$arguments); } $selfFunctions = get_class_methods($calledClass = static::class); if (!in_array($name, $selfFunctions)) { throw new BadMethodCallException("静的メソッド '{$name}' は {$calledClass} に存在しません。"); } 静的::$name(...$arguments) を返します。 } } クラス BaseModel は stdClass を拡張し、QueryableInterface、StorableInterface を実装します。 { クエリ可能 { を使用します Queryable::find as queryFind; } パブリック関数 find(mixed $primaryKeyValue): static { return self::queryFind($primaryKeyValue); } } <?php 名前空間 appStorageMethods; インターフェース StorableInterface { 関数 getObject(mixed $primaryKeyValue): 静的; 関数 find(mixed $primaryKeyValue): 静的; 関数 save(): 静的; }</pre> <p><br /></p>
P粉226642568
P粉226642568

全員に返信(1)
P粉439804514

Trait メソッドは「名前変更」できます: (実際にはエイリアス)

リーリー ######参照######

簡単な例: リーリー

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!