PHP Classes (original) (raw)

Last Updated : 19 Apr, 2025

A class defines the structure of an object. It contains properties (variables) and methods (functions). These properties and methods define the behavior and characteristics of an object created from the class.

**Syntax:

**Now, let us understand with the help of the example:

PHP `

`

Output

The class "GeeksforGeeks" was initiated!

**In this example:

**Creating Objects from Classes

Once a class is defined, you can create objects based on it. Here’s how you can create an object from a class:

$object = new ClassName('Hello', 'World');
echo $object->method1(); // Outputs: Hello World

**In this example:

Best practices for using the PHP classes

Conclusion

PHP classes are the foundation of Object-Oriented Programming. They allow you to model real-world entities, encapsulate data, and define behaviors that can be reused across your application. By using properties, methods, constructors, and taking advantage of inheritance and polymorphism, you can create robust and scalable PHP applications.