Page 1 of 1

Calling a Class imbedded in another Class

Posted: Thu May 13, 2004 4:17 pm
by hawleyjr
I have a class that has a few classes in its variables. I am trying to call a sub class of the main class. See below, it will probably make more sense then me trying to explain it.

Thanks -J

Code: Select all

<?php
//bdays
$a_event_id = array(1,5,6,4);
$a_event_label = array('label1','labe2','labe3','labe4');
$a_event_dttm = array('date1','date2','date3','date4');

$bdayEventClass = EventList();
	$bdayEventClass->setEventID($a_event_id);
	$bdayEventClass->setEventLabel($a_event_label);
	$bdayEventClass->setEventDTTM($a_event_dttm);

//sports
$a_event_id = array(5,5,6,6);
$a_event_label = array('label1','labe2','labe3','labe4');
$a_event_dttm = array('date1','date2','date3','date4');

$sportsEventClass = EventList();
	$bdayEventClass->setEventID($a_event_id);
	$bdayEventClass->setEventLabel($a_event_label);
	$bdayEventClass->setEventDTTM($a_event_dttm);

//hollidays
	//Same as above (bdays/hollidays - different content same idea)
//anniversaries
	//Same as above (bdays/hollidays - different content same idea)

$new_user = User();
	$new_user->setEvent_bdays($bdayEventClass);
	$new_user->setEvent_sports($sportsEventClass);
	$new_user->setEvent_hollidays($hollidayEventClass);
	$new_user->setEvent_anniversaries($anniversariesEventClass);

	//I would like to do this in one call? Is it possible? I remember from my days of Java programming it is I just do not remember how.
//GET THE SPORTS EVENT ID LIST
	$event_listClass = $new_user->getEvent_sports();
	$event_list = $event_listClass->getEventID();
	



class EventList{
	var $a_event_id;
	var $a_event_label;
	var $a_event_dttm;
	
	function EventList(){}
	
	function setEventID($x){$this->a_event_id=$x;}
	function setEventLabel($x){$this->a_event_label=$x;}
	function setEventDTTM($x){$this->a_event_dttm=$x;}
	
	function getEventID(){return $this->a_event_id;}
	function getEventLabel(){return $this->a_event_label;}
	function getEventDTTM(){return $this->a_event_dttm;}
}

class Users{
    var $user_id; 
    var $user_name; 
    var $event_bdays; 
    var $event_sports; 
    var $event_hollidays; 
    var $event_anniversaries; 
    
    function Users(){} 
    
    function getUser_id(){ return $this->user_id; } 
    function getUser_name(){ return $this->user_name; } 
    function getEvent_bdays(){ return $this->event_bdays; } 
    function getEvent_sports(){ return $this->event_sports; } 
    function getEvent_hollidays(){ return $this->event_hollidays; } 
    function getEvent_anniversaries(){ return $this->event_anniversaries; } 
    
    function setUser_id($x){ $this->user_id=$x; } 
    function setUser_name($x){ $this->user_name=$x; } 
    function setEvent_bdays($x){ $this->event_bdays=$x; } 
    function setEvent_sports($x){ $this->event_sports=$x; } 
    function setEvent_hollidays($x){ $this->event_hollidays=$x; } 
    function setEvent_anniversaries($x){ $this->event_anniversaries=$x; } 

}
?>

Posted: Thu May 13, 2004 4:30 pm
by PrObLeM
class EventList extends Users{

i think thats what you need...

Posted: Thu May 13, 2004 4:32 pm
by hawleyjr
What would the function call look like with using extends?

btw, thanks for the response.

Posted: Thu May 13, 2004 4:40 pm
by PrObLeM

Posted: Thu May 13, 2004 4:47 pm
by McGruff
By default, php v4 will copy objects rather than create a reference to them. Copies do not change state when another copy is manipulated. Almost always you don't want that, but rather references to a single instance.

Code: Select all

<?php

$object =& new Foo;

class Bar
{
    function Constructor(&$object)  // pass in to fn's by ref
    {
        $this->foo =& $object;  // assign to var by ref
    }

    function &getFoo()  // return from fn by ref
    {
        return $this->foo;
    }
}

?>
Not sure exactly what you meant by "sub-class". If you want to pass an object into another class, and use it's methods in methods of the second class, use references as above (php 4).

I'm not sure what you are encapsulating in the EventList class: it's basically just a hash (ie associative array - not the other meaning :) ). It can be useful to use classes like that occasionally but normally a class would encapsulate some behaviours as well - eg an EventsList class setEvent method might update a database.

Posted: Thu May 13, 2004 4:51 pm
by McGruff
PS: I'd probably go for aggregation (or composition) rather than inheritance. EventList isn't a type of user or vice versa.

Posted: Thu May 13, 2004 5:19 pm
by hawleyjr
McGruff wrote: I'm not sure what you are encapsulating in the EventList class: it's basically just a hash (ie associative array - not the other meaning :) )
At this point I wish it was the other meaning...

Yes, I could store the classes as in an array. However, (and this may just be my programming style) when I use arrays in that way I typically use constants for the array key. I do this because its easier to determine what I'm referencing.

Code: Select all

<?php
define('USER_ID',1);
define('USER_NAME',2);
define('EVENT_BDAYS',3);
define('EVENT_SPORTS',4);
define('EVENT_HOLLIDAYS',5);
define('EVENT_ANNIVERSARIES',6);

$a_Users = array(
	USER_ID => 1,
	USER_NAME => 'user name',
	EVENT_BDAYS =>$bdayEventClass,
	EVENT_SPORTS =>$sportsEventClass,
	EVENT_HOLLIDAYS =>$hollidayEventClass,
	EVENT_ANNIVERSARIES =>$anniversariesEventClass
	);
?>
I'll have to test this but will this even work?

Code: Select all

<?php
$a_user[EVENT_BDAYS]->getEventID(); 
?>

Posted: Thu May 13, 2004 6:13 pm
by launchcode
Nah, that won't work.

What is it you're trying to do? Access the $bdayEventClass object from within Users? You could just stick "global $bdayEventClass" within your User functions and then you'd be able to access any of the functions that class has available.

Posted: Thu May 13, 2004 6:28 pm
by hawleyjr
My main objective is to be able to call the class EventList that is being stored in the class Users.

What I wanted to do was avoid something that looked like this:

Code: Select all

<?php
$event_listClassBdays = $new_user->getEvent_bdays(); 
	$a_bday_event_id = $event_listClassBdays->getEventID(); 
	
$event_listClassSports = $new_user->getEvent_sports(); 
	$a_sports_event_id = $event_listClassSports->getEventID(); 
	
$event_listClassHollidays = $new_user->getEvent_hollidays(); 
	$a_hollidays_event_id = $event_listClassHollidays->getEventID(); 
	
$event_listClassAnniversaries = $new_user->getEvent_anniversaries(); 
	$a_anniversaries_event_id = $event_listClassAnniversaries->getEventID(); 
?>
I thought (and am wrong) that you could do something like this:

Code: Select all

<?php
$a_bday_event_id = $new_user->getEvent_bdays()->getEventID(); 
?>

Posted: Thu May 13, 2004 6:35 pm
by launchcode
Ok, in that case you need the EventList class to extend the User class, so that all of its functions are available on the User class level.

Posted: Thu May 13, 2004 6:47 pm
by hawleyjr
How would I call a specific function in the EventList Class?

The following variables located in the Users class store a different instance of the same class (EditList Class).

Code: Select all

<?php
    var $event_bdays; 
    var $event_sports; 
    var $event_hollidays; 
    var $event_anniversaries; 
?>
I can't just call:

Code: Select all

<?php
$new_user->getEventID(); 
?>

Posted: Thu May 13, 2004 7:57 pm
by McGruff
$new_user->event_bdays->getEventId();

Posted: Thu May 13, 2004 8:28 pm
by jason
hawleyjr: You do realize you can use arrays like so:

Code: Select all

$array['foo'] = "bar";

echo $array['foo']; // prints 'bar'
rather than using constants.

Posted: Thu May 13, 2004 10:06 pm
by hawleyjr
jason wrote:hawleyjr: You do realize you can use arrays like so:

Code: Select all

$array['foo'] = "bar";

echo $array['foo']; // prints 'bar'
rather than using constants.
Jason, Yes of course; PHP101.

When I'm implementing an array I use constants instead of string values mostly for error checking and readability. When I store the array in a table or reference it later on in the application it is much easier (for me at least) to know that $myArray[SOME_ARRAY_KEY] is the correct array value. If you have an array with 20+ keys and you are using text as key values I have found that there is a greater chance of error and confusion when using strings.

Now to play the devils advocate on myself. $myArray[‘SOME_ARRAY_KEY’] and $myArray[SOME_ARRAY_KEY] could just as easily be confused or misspelled.

I store my constants in an include file constants.inc.php I always know that the array key name is located there. So if later on in the code I’m implementing an array I don’t have to pull up the code that created the array to find out what each array key is, I can lookup the same file every time and locate all of the array keys for any array in my application. The majority of my applications are larger scale and I have found this to be the best method for me.

On another note (and I have no facts to back this up, it just makes sense to me) When PHP is compiling code and it comes to an array:

Code: Select all

<?php
$my_array = array(‘user_name’=>’John Doe’,’user_emai’=>’test@example.com’//….and so on…
?>
It has to go through each array key as a string it takes more time to read. On the other hand.

Code: Select all

<?php
define(‘USER_NAME’,1);
define(‘USER_EMAIL’,2);
…

$my_array = array(USER_NAME=>’John Doe’, USER_EMAIL=>’test@example.com’//….and so on…
?>
The array keys are integers which take less time for the compiler to go through.

Like I said earlier, I have no facts to back this up, it just makes sense to me. So if I’m wrong, which I may be. Call me on it. (Hey I’ve only had two Sierra Nevada BigFoots tonight)

BTW – Jason, Thanks for getting the forum back up. I too went through withdrawals.

… sorry for the long thread…

-J

Posted: Fri May 14, 2004 5:33 am
by launchcode
hawleyjr wrote: How would I call a specific function in the EventList Class?

I can't just call:

Code: Select all

<?php
$new_user->getEventID(); 
?>
Actually, you can - if the class is correctly extended then all of the functions are exposed and open to use. The problem here though is probably more a data design one than anything.