So I set up a project for myself to understand Classes and Method, that, if
people are willing to help, I think would really help me understand exactly
what is going on under the hood. I use functions regularly, as with PHP in
general. This though is something new to me and I believe will help me
expand my knowledge immensely! Here is what I am doing and what I am
starting from.
[code]
1. Create a collection of 10 fictional full names (last, and first) and give them ids (this could be an index)
[code=php]
/* from fictional_characters.php */
$fChars = array(
array(
‘uid’ => 0,
‘from’ => ‘Comic Book Guy on The Simpsons’,
‘name’ => ‘Jeff Albertson’,
),
array(
‘uid’ => 1,
‘from’ => ‘Barbies real name’,
‘name’ => ‘Barbara Roberts’,
),
array(
‘uid’ => 2,
‘from’ => ‘Sesame Street Mammoth’,
‘name’ => ‘Aloysius Snuffleupagus’,
),
array(
‘uid’ => 3,
‘from’ => ‘Peanuts Peppermint Patty’,
‘name’ => ‘Patricia Reichardt’,
),
array(
‘uid’ => 5,
‘from’ => ‘The Wizard of Oz’,
‘name’ => ‘Oscar Diggs’,
),
array(
‘uid’ => 6,
‘from’ => ‘Mr. Clean’,
‘name’ => ‘Veritably Clean’,
),
array(
‘uid’ => 7,
‘from’ => ‘The Man in the Yellow Hat from Curious George’,
‘name’ => ‘Ted Shackleford’,
),
array(
‘uid’ => 8,
‘from’ => ‘Monopoly mascot Rich Uncle Pennybags’,
‘name’ => ‘Milburn Pennybags’,
),
array(
‘uid’ => 9,
‘from’ => ‘The Skipper from Gilligans Island’,
‘name’ => ‘Jonas Grumby’,
),
array(
‘uid’ => 10,
‘from’ => ‘Shaggy from Scooby Doo’,
‘name’ => ‘Norville Rogers’,
),
);
This first part is easy and something I have done before, so on
to the next one where I start getting confused.
<i>
</i>2. Create a class called Characters that takes this collection/array as an initialization argument
[code=php]
<?php
include('fictional_characters.php');
class Characters {
function __construct($fChars) { /*****/ }
}
$obj = new Characters($fChars);
print_r($obj); //should return array?
?>
[/code]
[code=php]
class Characters
{
private $characters = array();
public function __construct($characters)
{
$this->characters = $characters;
}
}
[/code]
[code=php]
<?php
class FictionalCharacter
{
public $uid;
public $name;
public $from;
public function __construct($uid, $name, $from)
{
$this->uid = $uid;
$this->name = $name;
$this->from = $from;
}
}
class FictionalCharacterCollection
{
private $characters = array();
public function __construct($data = array())
{
foreach($data as $character) {
if(isset($character['uid']) && isset($character['name']) &&
isset($character['from'])) {
$this->add(new FictionalCharacter($character['uid'],
$character['name'],
$character['from']));
}
else {
throw new Exception("Invalid array for Fictional Character");
}
}
}
public function add(FictionalCharacter $character)
{
$this->characters[$character->uid] = $character;
}
public function getAll()
{
return $this->character;
}
public function getByUid($uid)
{
return (isset($data[$uid])) ? $data[$uid] : false;
}
}
[/code]
[code=php]
<?php
$cc = array(
array(
'uid' => 0,
'from' => 'Comic Book Guy on The Simpsons',
'first' => 'Jeff Albertson',
),
array(
'uid' => 1,
'from' => 'Barbies real name',
'first' => 'Barbara Roberts',
),
array(
'uid' => 2,
'from' => 'Sesame Street Mammoth',
'first' => 'Aloysius Snuffleupagus',
),
array(
'uid' => 3,
'from' => 'Peanuts Peppermint Patty',
'first' => 'Patricia Reichardt',
),
array(
'uid' => 5,
'from' => 'The Wizard of Oz',
'first' => 'Oscar Diggs',
),
);
class FictionalCharacter
{
public $uid;
public $name;
public $from;
public function __construct($uid, $name, $from)
{
$this->uid = $uid;
$this->name = $name;
$this->from = $from;
}
}
class FictionalCharacterCollection
{
private $characters = array();
public function __construct($data = array())
{
foreach($data as $character) {
if(isset($character['uid']) && isset($character['name']) &&
isset($character['from'])) {
$this->add(new FictionalCharacter($character['uid'],
$character['name'],
$character['from']));
}
else {
throw new Exception("Invalid array for Fictional Character");
}
}
}
public function add(FictionalCharacter $character)
{
$this->characters[$character->uid] = $character;
}
public function getAll()
{
return $this->character;
}
public function getByUid($uid)
{
return (isset($data[$uid])) ? $data[$uid] : false;
}
}
$obj = new FictionalCharacter($cc);
echo '<pre>'; print_r($obj); echo '</pre>';
?>
[/code]
<i>
</i>Warning: Missing argument 2 for FictionalCharacter::__construct(), called in C:xampplitehtdocslocal_test.php on line 76 and defined in C:xampplitehtdocslocal_test.php on line 37
Warning: Missing argument 3 for FictionalCharacter::__construct(), called in C:xampplitehtdocslocal_test.php on line 76 and defined in C:xampplitehtdocslocal_test.php on line 37
FictionalCharacter Object
(
[uid] => Array
(
[0] => Array
(
[uid] => 0
[from] => Comic Book Guy on The Simpsons
[first] => Jeff Albertson
)
<i> </i> [1] => Array
<i> </i> (
<i> </i> [uid] => 1
<i> </i> [from] => Barbies real name
<i> </i> [first] => Barbara Roberts
<i> </i> )
<i> </i> [2] => Array
<i> </i> (
<i> </i> [uid] => 2
<i> </i> [from] => Sesame Street Mammoth
<i> </i> [first] => Aloysius Snuffleupagus
<i> </i> )
<i> </i> [3] => Array
<i> </i> (
<i> </i> [uid] => 3
<i> </i> [from] => Peanuts Peppermint Patty
<i> </i> [first] => Patricia Reichardt
<i> </i> )
<i> </i> [4] => Array
<i> </i> (
<i> </i> [uid] => 5
<i> </i> [from] => The Wizard of Oz
<i> </i> [first] => Oscar Diggs
<i> </i> )
<i> </i> )
<i> </i>[name] =>
<i> </i>[from] =>
)
[code=php]function __destruct() {
print "Destructing ".$this->myvariable;
}
[/code]
[code=php]
<?php
require_once('classes/class_characters.php');
$fc = array(array('uid'=> 0,'from'=> 'Comic Book Guy on The Simpsons','name'=> 'Jeff Albertson',),array('uid' => 1,'from' => 'Barbies real name','name' => 'Barbara Roberts',),array('uid' => 2,'from' => 'Sesame Street Mammoth','name' => 'Aloysius Snuffleupagus',),array('uid' => 3,'from' => 'Peanuts Peppermint Patty','name' => 'Patricia Reichardt',),array('uid' => 5,'from' => 'The Wizard of Oz','name' => 'Oscar Diggs',),array('uid' => 6,'from' => 'Mr. Clean','name' => 'Veritably Clean',),array('uid' => 7,'from' => 'The Man in the Yellow Hat from Curious George','name' => 'Ted Shackleford',),array('uid' => 8,'from' => 'Monopoly mascot Rich Uncle Pennybags','name' => 'Milburn Pennybags',),array('uid' => 9,'from' => 'The Skipper from Gilligans Island','name' => 'Jonas Grumby',),array('uid' => 10,'from' => 'Shaggy from Scooby Doo','name' => 'Norville Rogers',));
$obj = new fictionalCharacters($fc);
print_r($obj);
?>
[/code]
[code=php]
<?php
class FictionalCharacter
{
public $uid;
public $name;
public $from;
public function __construct($uid, $name, $from)
{
$this->uid = $uid;
$this->name = $name;
$this->from = $from;
}
}
class FictionalCharacterCollection
{
private $characters = array();
public function __construct($data = array())
{
foreach($data as $character) {
if (isset($character['uid']) && isset($character['name']) && isset($character['from'])) {
$this->add(new FictionalCharacter($character['uid'], $character['name'], $character['from']));
} else {
throw new Exception("Invalid array for Fictional Character");
}
}
}
public function add(FictionalCharacter $character)
{
$this->characters[$character->uid] = $character;
}
public function getAll()
{
return $this->character;
}
public function getByUid($uid)
{
return (isset($this->characters[$uid])) ? $this->characters[$uid] : false;
}
}
// TEST
$fc = array(
array(
'uid' => 0,
'from' => 'Comic Book Guy on The Simpsons',
'name' => 'Jeff Albertson',
) ,
array(
'uid' => 1,
'from' => 'Barbies real name',
'name' => 'Barbara Roberts',
) ,
array(
'uid' => 2,
'from' => 'Sesame Street Mammoth',
'name' => 'Aloysius Snuffleupagus',
) ,
array(
'uid' => 3,
'from' => 'Peanuts Peppermint Patty',
'name' => 'Patricia Reichardt',
) ,
array(
'uid' => 5,
'from' => 'The Wizard of Oz',
'name' => 'Oscar Diggs',
) ,
array(
'uid' => 6,
'from' => 'Mr. Clean',
'name' => 'Veritably Clean',
) ,
array(
'uid' => 7,
'from' => 'The Man in the Yellow Hat from Curious George',
'name' => 'Ted Shackleford',
) ,
array(
'uid' => 8,
'from' => 'Monopoly mascot Rich Uncle Pennybags',
'name' => 'Milburn Pennybags',
) ,
array(
'uid' => 9,
'from' => 'The Skipper from Gilligans Island',
'name' => 'Jonas Grumby',
) ,
array(
'uid' => 10,
'from' => 'Shaggy from Scooby Doo',
'name' => 'Norville Rogers',
)
);
// instantiate a collection using that array:
$test = new FictionalCharacterCollection($fc);
// show what we now have:
echo "<pre>".print_r($test,1)."</pre>";
// lets grab one character and echo his/her name:
echo $test->getByUid(5)->name; // can chain them since getByUid() returns an object
[/code]
[code=php]
<?php
/**
* Defines a fictional character
*/
class FictionalCharacter
{
/**
* @var integer Character ID
*/
public $uid;
/**
* @var string Name of character
*/
public $name;
/**
* @var string Source of character
*/
public $from;
/**
* Constructor
* @return void
* @param int $uid
* @param string $name
* @param string $from
*/
public function __construct($uid, $name, $from)
{
$this->uid = $uid;
$this->name = $name;
$this->from = $from;
}
}
/**
* A collection of fictional character objects
*/
class FictionalCharacterCollection
{
/**
* @var array FictionalCharacter objects
*/
private $characters = array();
/**
* Constructor
* @return void
* @param array $data [optional] each element is array with keys 'uid',
* 'name', and 'from'
*/
public function __construct($data = array())
{
foreach($data as $character) {
if (isset($character['uid']) && isset($character['name']) && isset($character['from'])) {
$this->add(new FictionalCharacter($character['uid'], $character['name'], $character['from']));
} else {
throw new Exception("Invalid array for Fictional Character");
}
}
}
/**
* Add one character to the collection
* @return void
* @param object $character FictionalCharacter
*/
public function add(FictionalCharacter $character)
{
$this->characters[$character->uid] = $character;
}
/**
* Get the entire collection as an array of objects
* @return array
*/
public function getAll()
{
return $this->character;
}
/**
* Get one character by its ID
* @return object FictionalCharacter
* @param int $uid
*/
public function getByUid($uid)
{
return (isset($this->characters[$uid])) ? $this->characters[$uid] : false;
}
}
[/code]
I'm a bit confused over the need
of args two and three of the class, since they're already in the
array.[/quote]
[code=php]
$obj = new fictionalCharacter($fc, null, null); //$fc is the array
print_r($obj);
[/code]
[code=php]
$obj = new fictionalCharacter(9, 'John Doe', 'A male party whose true identity is unknown or must be withheld for legal reasons.');
print_r($obj);
[/code]
[code=php]
/**
* Defines a fictional character
*/
class FictionalCharacter
{
/**
* @var integer Character ID
*/
public $uid;
/**
* @var string Name of character
*/
public $name;
/**
* @var string Source of character
*/
public $from;
/**
* Constructor
* @return void
* @param mixed Either $uid, $name, $from OR array
*/
public function __construct()
{
switch(func_num_args()) {
case 3:
$this->uid = func_get_arg(0);
$this->name = func_get_arg(1);
$this->from = func_get_arg(2);
break;
case 1:
list($uid, $name, $from) = func_get_arg(0);
$this->uid = $uid;
$this->name = $name;
$this->from = $from;
default:
throw new Exception("Incorrect number of arguments");
}
}
}
[/code]
Or a traditional get/set for each so the sequence isn't important (nor is the constructor, for that matter).[/QUOTE]
0.1.9 — BETA 6.18