How to create and use objects

How to create an object

The syntax

$objectName = new ClassName(argumentList);

Create a Category

object $brass = new Category(4, 'Brass');

Create a Product object

$trumpet = new Product($brass, 'Getzen', 'Getzen 700SP Trumpet', 999.95);

How to access an object’s properties

The syntax for setting a public property value

$objectName->propertyName = value;

The syntax for getting a public property value

$objectName->propertyName;

Set a property

$trumpet->comment = 'Discontinued';

Get a property

echo $trumpet->comment;

How to call an object’s methods

The syntax

$objectName->methodName(argumentList);

Call the getFormattedPrice() method

$price = $trumpet->getFormattedPrice();

Object chaining

echo $trumpet->getCategory()->getName();

Description

Back