Writing Classes with PHP
Classes allow for the creation of objects that can contain multiple properties. The properties can then be pulled from the class using methods for a specific object. I recently wanted to create an array of objects, so I created an index.php page that looked similar to this.
<?php include("class_lib.php"); for ($i=0; $i < 10; $i++){ // Use data pulled from a database or post data $array[$i] = new person ($name,$age,$address); } var_dump($array); ?>
I first include the class I am going to use to create the objects and then I create a new person for each instance of the array. I suggested that you get the data from post data or from a database.
The class will then look something like this.
<?php class person{ private $name; private $age; private $address; public function __construct($persons_name, $persons_age, $persons_address){ $this->name = $persons_name; $this->age = $persons_age; $this->address = $persons_address; } // You can create set methods like public function set_name($new_name) { $this->name = $new_name; } //Or you can create get mehthods like public function get_name(){ return $this->name; } } ?>
If you still have questions, then a good resource is to go to killerphp.com