PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Nov 21, 2004 6:03 pm
Code: Select all
<?php
$includes = array('nav','template');
foreach ($includes as $included)
{
include('classes/class.'.$included.'.php');
$included = new $included.'()';
}
?>
I have several classes I want to include, each on seperate pages.
How can I do something like the following
I get the following error
Fatal error: Call to a member function includepage() on a non-object in C:\apache2triad\htdocs\index_temp.php on line 24
peni
Forum Commoner
Posts: 34 Joined: Thu Nov 18, 2004 1:15 pm
Post
by peni » Sun Nov 21, 2004 6:11 pm
must depend on your classes and their constructors, probably.
such kind of declaration works fine at my pc:
Code: Select all
$clsName = "myClass";
$myObj = new $clsName;
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Nov 21, 2004 6:36 pm
the problem is that the object name has to be the same..
look at my code above
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sun Nov 21, 2004 7:04 pm
if i understand well:
Code: Select all
foreach($includes as $included)
{
include('classes/class.'.$included.'.php'););
$included = new $included;
}
seems to work overhere (PHP5)...
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Nov 21, 2004 7:21 pm
im running PHP5.. and according to the late master feyd he sais it will require a singleton becuase I will lose the previous instance each time it is looped.
crabyars
Forum Commoner
Posts: 37 Joined: Thu Jun 17, 2004 8:24 pm
Post
by crabyars » Sun Nov 21, 2004 8:48 pm
Could you use the object in an array? then maybe you can do something like this:
Code: Select all
<?php
$includes = array('nav','template');
$objArray = array();
foreach ($includes as $included)
{
include('classes/class.'.$included.'.php');
$objArray[] = new $included.'()';
}
?>
Or, perhaps you need to de-reference the declaration name (see extra $ on line 8):
Code: Select all
<?php
$includes = array('nav','template');
foreach ($includes as $included)
{
include('classes/class.'.$included.'.php');
$$included = new $included.'()';
}
?>
With the second example, I imagine you end up with 2 object named $nav and $template