PHP Classes

A. General Instantiatable class


namespace App\Test;

use App\Traits\FirstTrait;
use App\Traits\SecondTrait;

class ClassName extends ParentClass implements FirstInterface, SecondInterface
{
    use FirstTrait;
    use SecondTrait;
    use ThirdTrait {
        SecondTrait::render insteadof ThirdTrait;
        ThirdTrait::make as parentMake;
    }

    public const string PUBLIC_CONSTANT = 'anywhere';
    protected const string PROTECTED_CONSTANT = 'in class and children';
    private const string PRIVATE_CONSTANT = 'only here';

    public static string $staticPublic = 'Public - Static';
    protected static string $staticProtected = 'Protected - Static';
    private static string $staticPrivate = 'Private - Static';

    public string $public = 'Public';
    protected string $protected = 'Protected';
    private string $private = 'Private';

    public readonly bool $readonlyProperty;
    public $mixed;

    public string $virtualProperty {
        get => strtoupper($this->propertyHook);

        set(string $value) {
            $this->propertyHook = strtolower($value);
        }
    }

    public string $propertyHook = 'default value' {
        get {
            return $this->propertyHook;
        }
        set(string $value) {
            $this->propertyHook = strtolower($value);
        }
    }

    public function __construct(
        public private(set) string $title,
        public protected(set) string $author,
        protected private(set) int $pubYear,
    ) {
        //
        $this->readonlyProperty = true;
    }

    public static function echo(): void
    {
        static::echoConstants();
        static::echoStaticProperties();
    }

    public function print(): void
    {
        echo static::PUBLIC_CONSTANT;
        echo static::PROTECTED_CONSTANT;
        echo static::PRIVATE_CONSTANT;

        $this->printStaticProperties();
        $this->printProperties();
    }

    protected static function echoConstants(): void
    {
        echo static::PUBLIC_CONSTANT;
        echo static::PROTECTED_CONSTANT;
        echo static::PRIVATE_CONSTANT;
    }

    private static function echoStaticProperties(): void
    {
        echo self::$staticPublic;
        echo self::$staticProtected;
        echo self::$staticPrivate;
    }

    protected function printStaticProperties(): void
    {
        echo static::$staticPublic;
        echo static::$staticProtected;
        echo static::$staticPrivate;
    }

    private function printProperties(): void
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }

    public function foo($firstArg, string $stringArg, int|float $twoPartArg, ?string $defaultArg = null): ?string
    {
        return 'a string';
    }

    public function bar(
        FirstClass|SecondClass|array $firstArg,
        string $stringArg,
        int|float $twoPartArg,
        ?string $defaultArg = null
    ): array|null {

        return [];
    }
}

B. Abstract class

All functionality of a general class is available plus abstract methods.


namespace App\Test;

abstract class ClassThatMustBeExtended
{
    abstract public static function whoAmI(): string
    abstract protected static function myName(): string

    abstract public function helloWorld(string $message): string
    abstract public function helloServer(string $message): string
}

C. Final class

Final classes can not be extended.


namespace App\Test;

final class ClassThatCannotBeExtended
{
    //
}