How to write a simple object in PHP

This is a fairly simple question, but I could not find the correct answer.

Let's say I write in ActionScript 3 as an object:

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

How can I write such a thing in PHP

+8
variables object php
source share
2 answers
 $myCar = new stdClass; $myCar->engine = 'Nice Engine'; $myCar->numberOfDoors = 4; $myCar->howFast = 150; 

See the documentation for objects for a more in-depth discussion.

+19
source share

You can use classes, for example:

 class Car { public $engine; public $numberOfDoors; public $howFast; } $myCar = new Car(); $myCar->engine = 'Nice Engine'; $myCar->numberOfDoors = 4; $myCar->howFast = 150; 

or if you need this object only for storing properties, you can use an associative array, for example:

  $myCar['engine'] = "Nice engine"; $myCar['numberOfDoors'] = 4; $myCar['howFast'] = 150; 
+8
source share

All Articles