final class PhpFile

Definition of a PHP file.

Generates:

  • opening tag (<?php)
  • doc comments
  • one or more namespaces

Methods

static PhpFile
fromCode(string $code)

No description

addClass(string $name)

Adds a class to the file. If it already exists, throws an exception.

addInterface(string $name)

Adds an interface to the file. If it already exists, throws an exception.

addTrait(string $name)

Adds a trait to the file. If it already exists, throws an exception.

addEnum(string $name)

Adds an enum to the file. If it already exists, throws an exception.

addFunction(string $name)

Adds a function to the file. If it already exists, throws an exception.

addNamespace(PhpNamespace $namespace)

Adds a namespace to the file. If it already exists, it returns the existing one.

addBody(string $code, array|null $args = null)

No description

string
getBody()

No description

removeNamespace(PhpNamespace $namespace)

Removes the namespace from the file.

array
getNamespaces()

No description

array
getClasses()

No description

array
getFunctions()

No description

addUse(string $name, string|null $alias = null, string $of = PhpNamespace::NameNormal)

Adds a use statement to the file, to the global namespace.

setStrictTypes(bool $state = true)

Adds declare(strict_types=1) to output.

bool
hasStrictTypes()

No description

string
__toString()

No description

Details

at line 34
static PhpFile fromCode(string $code)

No description

Parameters

string $code

Return Value

PhpFile

at line 44
ClassType addClass(string $name)

Adds a class to the file. If it already exists, throws an exception.

As a parameter, pass the full name with namespace.

Parameters

string $name

Return Value

ClassType

at line 56
InterfaceType addInterface(string $name)

Adds an interface to the file. If it already exists, throws an exception.

As a parameter, pass the full name with namespace.

Parameters

string $name

Return Value

InterfaceType

at line 68
TraitType addTrait(string $name)

Adds a trait to the file. If it already exists, throws an exception.

As a parameter, pass the full name with namespace.

Parameters

string $name

Return Value

TraitType

at line 80
EnumType addEnum(string $name)

Adds an enum to the file. If it already exists, throws an exception.

As a parameter, pass the full name with namespace.

Parameters

string $name

Return Value

EnumType

at line 92
GlobalFunction addFunction(string $name)

Adds a function to the file. If it already exists, throws an exception.

As a parameter, pass the full name with namespace.

Parameters

string $name

Return Value

GlobalFunction

at line 103
PhpNamespace addNamespace(PhpNamespace $namespace)

Adds a namespace to the file. If it already exists, it returns the existing one.

Parameters

PhpNamespace $namespace

Return Value

PhpNamespace

at line 118
PhpFile addBody(string $code, array|null $args = null)

No description

Parameters

string $code
array|null $args

Return Value

PhpFile

at line 129
string getBody()

No description

Return Value

string

at line 138
PhpFile removeNamespace(PhpNamespace $namespace)

Removes the namespace from the file.

Parameters

PhpNamespace $namespace

Return Value

PhpFile

at line 147
array getNamespaces()

No description

Return Value

array

at line 154
array getClasses()

No description

Return Value

array

at line 169
array getFunctions()

No description

Return Value

array

at line 186
PhpFile addUse(string $name, string|null $alias = null, string $of = PhpNamespace::NameNormal)

Adds a use statement to the file, to the global namespace.

Parameters

string $name
string|null $alias
string $of

Return Value

PhpFile

at line 196
PhpFile setStrictTypes(bool $state = true)

Adds declare(strict_types=1) to output.

Parameters

bool $state

Return Value

PhpFile

at line 203
bool hasStrictTypes()

No description

Return Value

bool

at line 209
string __toString()

No description

Return Value

string

Traits