Re: Array of objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thomas Hochstetter wrote:
Hi Jochem,

Thanks for that. The eval story was just a desperate attempt ... anyway, I
know references a bit from my c++ times. I am not sure how that will help in
this case though. Maybe you want to share with me your thoughts on that.

well compare:

$a =& new Test;	// $a is the new object
$b = new Test;	// $b is a copy of the new object
$c =& $b;	// $c refers to the same object
$d = $b;	// $d is a seperate copy of $b

by default in php4 every time you assign an object (new or otherwise) to
a variable you are making a COPY, the same goes for when you pass an object
to a function.

There is shedloads of info around that explains the how & why of references
in detail, just google around a bit.

I have no idea about c++ (the C++ book beside the bed is only half-read and
hardly understood ;-) so whether your understanding of references in c++ is a
help or hinderance I can guess at :-)


Just for completion's sake:

I want to have an array in a config file that holds all possible modules
that can be loaded. The module name comes from the url string and if the
string is not in that array I display a default page (so no tinkering can go
on).
The reason for doing what you helped me doing now is only a cosmetic reason.
Instead of having to declare another block with if and else if statements to
instantiate the objects as in:

if( $Site == null ) {
if ( $module=="browse" ) { $Site = new
Browse(); }
else if ( $module=="details" ) { $Site = new Details();} }


I thought it would be cool to be able to instantiate the object when I check
the array (on finding the right entry).

I might have written that as a function e.g.

function getModule( $modName )
{
	if (in_array($modName, $modules)) {
		$modClass = $modules[ $modName ];
		return new $modClass();
	}
	
	return new DefaultModule();
}

or possible a switch statement, which would be a more concise way of
writing a big if/else block

switch ($_GET['module']) {
	case 'browse':
		$Site = new Browse();
		break;
	case 'details':
		$Site = new Details();
		break;
	default:
		$Site = new DefaultModule();
		break;
}

... or a combination of the two (examples above).

btw, there is nothing implicitly wrong with the boring old if/else block...
it just maybe doesn't have the coolness factor, regardless eval() really doesn't
sound like the way to go here at all. which ever way you do it, the most important
thing is to layout you code _neatly_ (don't be afraid of using a bit of space :-)
and comment as much as possible so that maintainance of the code is as easy as
possible.

Good Luck! :-)

PS - please reply to list as well - somebody might be interested (and learn something).


Thanks again.

Cheers

Thomas

-----Original Message-----
From: Jochem Maas [mailto:jochem@xxxxxxxxxxxxx] Sent: 06 May 2005 01:17 PM
To: Thomas Hochstetter
Cc: php-general@xxxxxxxxxxxxx
Subject: Re: Array of objects


Thomas Hochstetter wrote:

Spiraleye.Studios

 Hi there,

I need help with arrays. What I want to do is to have an array of the following structure:

$mod=array( 'name'=>new NameObject());

^ -- looks like your single quotes got mangled in my email client.

class Test { function Test($str = '') { echo $str; } }

$_GET['module'] = 'testmod';
$mod = array( 'testmod' => 'Test' );

if (isset($mod[$_GET['module']]) && class_exists($c =
$mod[$_GET['module']])) {
     $Site = new $c;
     // $Site = new $c('Testing');
} else {
     die ('Can't create module object');
}


... and if that doesn't give you the knowledge/inspiration/lighbulb-moment to fix whatever it is your trying to do then please mail the list again and explain what _and_ why you are trying to do whatever it is you're trying to do (this helps people to understand thereby having a better chance of offering some helpful advice!)


Then later in the page I want to go $Site = $mod[$_GET['module']] (or something like that) to instantiate a new object.

The problem is, if done the way above it will try to instantiate the object right there and then in the array (defeating the purpose), if I put it in quotes and try to use something like:

(object)eval($mod[$_GET['module']]) it does not instantiate the object.


well assuming no syntax error occurs (eval will return false is such a case)
then I bet it actually does, only your eval statement isn't assigning the
new object to anything!... besides which you are casting the return
value of eval to an object which is pointless for 2 reasons:

1. casting NULL to an object gets you nowhere (atleast not in this case)
2. casting an object to an object get you... nowhere :-)

so....:

$mod = array( 'name' => 'return new NameObject()');
$Site = eval($mod['name']);

PS - do you know what a reference is? (in terms of [php] variables)?
understanding
references with regard to objects (especially if your using php4) is a
really
good addition to your coding arsenal (it will save you time hunting
inexplicable bugs -
often such 'bugs' are nog longer inexplicable! and allow you to increase the
speed of your
code)

PPS - using eval() is hardly ever the only option, and given that eval() is
very slow its best
to avoid it at all costs (assuming there is another way to do what you
want)... also eval()
_can_ be a security risk, if you do something like:

eval($_GET['command'])

without santizing the contents of $_GET['command'], hopefully you can see
why.



Is there a way to do this?

Thanks

Thomas

*
**SPIRAL EYE STUDIOS ***
P.O. Box 37907, Faerie Glen, 0043

Tel: +27 12 362 3486
Fax: +27 12 362 3493
Mobile: +27 83 258 2669
Email: info@xxxxxxxxxxxxxxx <mailto:info@xxxxxxxxxxxxxxx>
Web: www.spiraleye.co.za <http://www.spiraleye.co.za>






-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux