When you start with object-oriented programming in PHP, the ability to create classes and objects is essential. In this tutorial, you will learn how to create instances of classes in PHP while working with the keyword "new". This lays the foundation for developing complex and structured applications.
Key Insights
- Classes are blueprints for objects that define specific properties and methods.
- Instances of these classes are created using the keyword "new".
- Accessing properties and methods in PHP works differently than in some other programming languages.
- Working with instances is fundamental to object-oriented programming in PHP.
Step-by-Step Guide
1. Defining a Class
Before you can create an instance of a class, you must first define a class. A class contains properties that represent the state of the object and methods that define the behavior of the object. You begin by clarifying the structure of your class.

2. Creating an Instance with "new"
Once the class has been defined, it's time to create an instance. You use the keyword "new", followed by the class name, to instantiate a new object. This instance is stored in a variable that allows you to access the properties and methods of this class.

3. Accessing Properties of the Instance
To access the properties of an instance, you use the special PHP syntax that defines access to these elements. Instead of a dot, as is the case in some other programming languages, here you use the arrow followed by the angle bracket.
This gives you access to the "type" property of your instance.

4. Accessing Methods of the Instance
Similarly to accessing properties, you can also call methods that are defined in the class. You also use the arrow and angle bracket syntax to address a method, such as getType().
5. Displaying Results in the Browser
To ensure everything is working correctly, it is helpful to display the results in a browser. You can add an HTML line break to structure the output. Then you open the file in the browser and check the display. The output shows you the content of the properties and the result of the method. This helps you ensure that your instance is working correctly.

6. Basic Summary
You have now learned the basic steps to work with classes and instances in PHP. You can define classes, create objects, and access their properties and methods using specific syntax. You are well-equipped to further explore object-oriented programming in PHP.

Summary – From Class to Object: Instances in PHP with "new"
In this tutorial, you learned how to define classes in PHP and create instances of these classes using the keyword "new". Additionally, you learned how to access properties and methods, and the result displayed in the browser helps you verify the implementation.
Frequently Asked Questions
How do I define a class in PHP?You start with the keyword "class" followed by the class name and open a block to define properties and methods.
How do I create an instance of a class?Use the keyword "new" followed by the name of the class to create a new instance.
How do I access properties of a class?Use the syntax ->, followed by the property name to access them.
How do I call a method of a class?Use the same -> syntax followed by the method name and add parentheses to call the method.