Objects are a data structure that has both properties (data) and methods (functions). We create a Class, the definition of the object, and then we can create instances of the class (variables).
We can use OOP notation very easily without actually creating a Class. For instance we could say
$ny->capital="Albany"; $ny->name="New York";
We have created an instance of an object called $ny that has two properties or pieces of data, the name and the capital. We can use this instance variable the same way that we would any pother variable:
if($ny->capital=="New York") ...We can also create instances that are elements of an array:
$state['NY']->name="New York"; $state['NY']->capital="Albany";
Although we have created an instance of the generic object, we have not actually created a class. The code below will create a new class called State:
class State {
var $name;
var $capital;
function setValue($name,$capital) {
$this->name=$name;
$this->capital=$capital;
} //setValue
function information() {
return 'The capital of '.$this->name.' is '.$this->capital.'.';
} //information
} //State
We can then declare a variable as a new State and use the two methods setValue and information as show below:
$ny = new State();
$ny->setValue("New York","Albany");
echo $ny->information();