Skip to content
Skills Library
Programming · Learning & explanation

php-oop

/php-oop

ACTIVATE when designing PHP classes, value objects, collections, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' in PHP. Provides PHP-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus PHP-specific patterns (readonly properties, IteratorAggregate). DO NOT use for: refactoring methodology (see php-refactoring), DDD domain modeling (see php-ddd-conventions).

Licence
MIT
Last repository change
12.10.2026

Assessment and security

95/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content30 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 12.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

ACTIVATE when designing PHP classes, value objects, collections, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' in PHP. Provides PHP-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus PHP-specific patterns (readonly properties, IteratorAggregate). DO NOT use for: refactoring methodology (see php-refactoring), DDD domain modeling (see php-ddd-conventions).

Triggers from the description

Tell Don
Original author description

ACTIVATE when designing PHP classes, value objects, collections, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' in PHP. Provides PHP-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus PHP-specific patterns (readonly properties, IteratorAggregate). DO NOT use for: refactoring methodology (see php-refactoring), DDD domain modeling (see php-ddd-conventions).

How it works

  1. Example — Rule 1: Tell Don't Ask
  2. Example — Rule 2: Collection Over Named Properties
  3. Example — Rule 3: Whole Object
  4. Example — Rule 4: Iterable Collections via IteratorAggregate
  5. Example — Rule 5: Self-Describing Value Object
Original SKILL.md headings · 100 lines, 344 words
SKILL.md preview
---
name: php-oop
description: "ACTIVATE when designing PHP classes, value objects, collections, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' in PHP. Provides PHP-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus PHP-specific patterns (readonly properties, IteratorAggregate). DO NOT use for: refactoring methodology (see php-refactoring), DDD domain modeling (see php-ddd-conventions)."
---

# OOP — PHP Examples

> The **rules** are defined in `craft:oop-principles`. This skill provides PHP / Symfony examples plus PHP-specific patterns.

## Example — Rule 1: Tell Don't Ask

```php
// ❌ AVOID — caller queries and decides
foreach ($identityDocument->getRequiredFieldNames() as $fieldName) {
    $file = $identityDocument->getFileByFieldName($fieldName);
    if ($file !== null) { continue; }
    // ... re-download logic
}

// ✅ CORRECT — object exposes behavior
foreach ($identityDocument->getMissingExistingFiles() as $fieldName => $existingFile) {
    // Object already determined which files are missing
}
```

## Example — Rule 2: Collection Over Named Properties

```php
// ❌ AVOID — N separate properties = N identical code paths
final class FormData {
    private ?File $frontFile = null;
    private ?File $backFile = null;
    private ?File $passportFile = null;
}

// ✅ CORRECT — one keyed collection
final class FormData {
    /** @param array<string, File> $files */
    private array $files = [];
    public function addFile(string $fieldName, File $file): void { /* ... */ }
}
```

## Example — Rule 3: Whole Object

```php
// ❌ AVOID — caller destructures
$collection->add(
    documentType: $document->type,
    documentName: $document->originalFileName,
    downloadUrl: $url,
);

// ✅ CORRECT — pass the whole object
$collection->addFromDocument($document, $url);
```

## Example — Rule 4: Iterable Collections via `IteratorAggregate`

PHP-specific: implement `IteratorAggregate` to allow `foreach` while keeping internals private.

```php
/** @implements \IteratorAggregate<string, FileInfo> */
final class FilesCollection implements \IteratorAggregate
{
    public function __construct(private array $files = []) {}

    public function add(string $name, FileInfo $file): void
    {
        $this->files[$name] = $file;
    }

    public function getIterator(): \ArrayIterator
    {
        return new \ArrayIterator($this->files);
    }
}

// Usage: foreach ($collection as $name => $file) { ... }
```

## Example — Rule 5: Self-Describing Value Object

```php
// ❌ AVOID — consumer needs external fieldName → fileType mapping
final class UploadFile {
    public function __construct(
        public string $content,
        public string $originalFileName,
    ) {}
}

// ✅ CORRECT — object carries its own type
final readonly class UploadFile {
    public function __construct(
        public FileTypeEnum $type,
        public string $content,
        public string $originalFileName,
    ) {}
}
```

Readonly placement (per-property vs class-level) is `php-8-2`'s call, not this skill's.

Versions

The database has no other skills named php-oop.

Database snapshot: 13.11.2026 · package version 4641b63fe786 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95oop-principles
    ACTIVATE when designing classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' across languages. Covers cross-language OOP principles: Tell Don't Ask, collection over named properties,…
  • 95ts-oop
    ACTIVATE when designing TypeScript classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, Tell Don't Ask, or Symbol.iterator. Provides TS-specific examples for the cross-language OOP rules defined in…
  • 90oop-guide
    Use when designing class hierarchies or applying OOP principles. Triggers on prompts about classes, inheritance, polymorphism, encapsulation, SOLID, design patterns, or interfaces, even when the user doesn't say 'OOP'.
  • 100php-idioms
    Modern PHP 8.x idioms: strict types, typed properties, enums, readonly classes, match expressions, and PSR standards. Use when writing, refactoring, or reviewing pure PHP code, domain services, or libraries. Pair with laravel-idioms for Laravel applications.
  • 100php-language
    Apply core PHP language standards and modern 8.x features. Use when working with PHP 8.x features like enums, fibers, readonly properties, or named arguments.
  • 90php-pro
    Write idiomatic PHP code with generators, iterators, SPL data structures, and modern OOP features. Use for high-performance PHP applications.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.