Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. class / new / $this

class / new / $this Since: PHP 5(2004)

The basic syntax for defining a class and creating objects. It forms the foundation of object-oriented programming, which groups related data and behavior together.

Syntax

// Define a class.
class ClassName {
	// Property declaration
	public $property;

	// Constructor
	public function __construct($param) {
		$this->property = $param;
	}

	// Method definition
	public function method() {
		return $this->property;
	}
}

// Create an instance.
$obj = new ClassName($value);

Syntax Reference

SyntaxDescription
classThe keyword used to define a class. Creates a blueprint that groups properties and methods together.
newThe keyword used to create an instance from a class. When an instance is created, __construct() is called automatically.
$thisA special variable that refers to the current instance. Use it inside methods to access the object's own properties and methods.
__construct()The constructor. Runs automatically when an instance is created with new, and is used to initialize the object.
__destruct()The destructor. Runs automatically when an object is destroyed, and is used for cleanup tasks.

Sample Code

<?php
// Basic class definition.
class User {
	public string $name;
	public int $age;

	public function __construct(string $name, int $age) {
		$this->name = $name;
		$this->age = $age;
	}

	public function greet(): string {
		return "Hello, I'm {$this->name}. I'm {$this->age} years old.";
	}
}

$user = new User("Taro", 25);
echo $user->greet(); // Outputs: "Hello, I'm Taro. I'm 25 years old."
echo $user->name;    // Outputs: "Taro"

// Using constructor promotion introduced in PHP 8.0 for more concise code.
class Product {
	public function __construct(
		public readonly string $name,
		public readonly int $price,
		public readonly int $stock = 0
	) {}

	public function format_price(): string {
		return "¥" . number_format($this->price);
	}
}

$product = new Product("PHP Textbook", 2980, 50);
echo $product->format_price(); // Outputs: "¥2,980"
echo $product->name;           // Outputs: "PHP Textbook"

// Example using a destructor.
class FileHandler {
	private $handle;

	public function __construct(string $path) {
		$this->handle = fopen($path, 'r');
		echo "File opened.\n";
	}

	public function __destruct() {
		if ($this->handle) {
			fclose($this->handle);
		}
		echo "File closed.\n";
	}
}

$file = new FileHandler("/tmp/test.txt");
unset($file); // The destructor runs here.

// Example of method chaining.
class QueryBuilder {
	private string $table = '';
	private string $where = '';

	public function from(string $table): self {
		$this->table = $table;
		return $this; // Returning $this enables chaining.
	}

	public function where(string $condition): self {
		$this->where = $condition;
		return $this;
	}

	public function build(): string {
		return "SELECT * FROM {$this->table} WHERE {$this->where}";
	}
}

$sql = (new QueryBuilder())->from('users')->where('age > 20')->build();
echo $sql; // Outputs: "SELECT * FROM users WHERE age > 20"

Notes

A class is the fundamental unit of object-oriented programming — a blueprint that groups related data and the methods that operate on it. When you create an instance with the new keyword, __construct() runs automatically to set up initial values.

Constructor promotion, introduced in PHP 8.0, lets you declare, type-hint, and assign a property all in one line. Adding the readonly modifier prevents the property from being changed after initialization, which is useful for creating immutable objects.

For access modifiers, see public / private / protected. For inheritance, see extends / implements. For traits, see trait.

If you find any errors or copyright issues, please .