Properties

Aka attributes 

  • Data type definitions for storing the actual values to be assigned to an individual object
  • Must be defined with a visibility keyword: public, protected, private, final
  • Can also be initialised by assigning a value within the class definition

 

This example shows a property being added to the class:

<?php

class Bike {

	public $make ;
}

echo "<h1>Instantiating a Bike Object</h1>" ;

$myBike = new Bike ;

echo "<p>A new Bike object has been instantiated, called \$myBike</p>";

//the -> (small arrow) access operator is used to get or set an object's attributes
$myBike->make = "Ducati" ; //$ sign is not required for variable when using ->

echo "<p>The \$myBike object's single property, \$make, has been assigned a value of: ";

echo $myBike->make . "</p>";

echo "<pre>" . var_export($myBike, TRUE) . "</pre>" ;

?>

Save & refresh browser:

Instantiating a Bike Object

A new Bike object has been instantiated, called $myBike

 

The $myBike object's single property, $make, has been assigned a value of: Ducati

 

Bike::__set_state(array(

'make' => 'Ducati',
))

 

 

*Note: the -> (aka small arrow) access operator is used to get or set an object's attributes

Leave a Reply