Syntax

PHP Define a Class Syntax

In PHP, define a class: defines a class with constructor promotion — class Dog { public function __construct(public string $name) {} }

Quick Answer

Syntax
class Dog { public function __construct(public string $name) {} }
PHPSyntax

Define a Class

Code example
class Dog { public function __construct(public string $name) {} }

Defines a class with constructor promotion.

Learn More

How to define a class in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Define a Class Syntax

Use class Dog { public function __construct(public string $name) {} }.

Related Syntax References

Frequently asked questions

What is the PHP syntax for define a class?

Use this pattern: class Dog { public function __construct(public string $name) {} }

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify