Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a simple object in PHP

this is quite an easy question but I couldn't seem to find a proper answer.

Let's say I am writing in actionScript 3 an object like this:

var myCar = new Object();
myCar.engine = "Nice Engine";
myCar.numberOfDoors = 4;
myCar.howFast= 150;

how do I write such a thing in PHP?

like image 532
Alon Avatar asked Nov 30 '11 13:11

Alon


People also ask

What is object in PHP with example?

In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.

Can we create object without class in PHP?

We can create an object without creating a class in PHP, typecasting a type into an object using the object data type. We can typecast an array into a stdClass object.

How define class and create its object in PHP?

Define Objects Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values. Objects of a class is created using the new keyword.

Is object a function in PHP?

The is_object() function checks whether a variable is an object. This function returns true (1) if the variable is an object, otherwise it returns false/nothing.


1 Answers

$myCar = new stdClass;
$myCar->engine = 'Nice Engine';
$myCar->numberOfDoors = 4;
$myCar->howFast = 150;

Have a look at the documentation for objects for a more in-depth discussion.

like image 117
Clive Avatar answered Sep 17 '22 09:09

Clive