Newbie Question: Creating Objects

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

Post Reply
seem
Forum Newbie
Posts: 6
Joined: Fri Jun 28, 2002 9:48 am
Location: Munich, Germany

Newbie Question: Creating Objects

Post by seem »

Hello Everyone,

for a web-based computer managment application I've tried to use a file-structur similar to this:

/php/database ------------ DbHosts.php ------------- getHostsByName ($hostname)
\ \------------ getHostByIpAdress ($ip)
\---- Hosts.php ------ setHostname($hostname)
\----- getHostname()


How can I set a classpath or something similar to mark the location of all classes. If I am in /php/database/DbHosts.php and I want to get an instance from /php/Hosts.php I have now idea how I can handle this. (something like $hosts = new ../Hosts ;-))

Thanks in advance

Andy
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Nope, you include each of the class files that you want to use.

Code: Select all

include_once 'class1.php';
include_once '../class2.php';
Then you can call new instances of the classes defined in those files:

Code: Select all

$new = new Class1;
If you use include_once() it solves problems where class files are included multiple times because they're included in one file and also included in a file that that page includes...

Mac
seem
Forum Newbie
Posts: 6
Joined: Fri Jun 28, 2002 9:48 am
Location: Munich, Germany

Post by seem »

Thank you for your help, but I get the following errors:

Warning: Unable to access ../database/DbHost.php in /usr/local/httpd/htdocs/cat/php/rendering/searchHostResult.php on line 3

Warning: Failed opening '../database/DbHost.php' for inclusion (include_path='') in /usr/local/httpd/htdocs/cat/php/rendering/searchHostResult.php on line 3

Fatal error: Cannot instantiate non-existent class: dbhost in /usr/local/httpd/htdocs/cat/php/rendering/searchHostResult.php on line 5

If I switch the safe mode off The first Warning disappears. I gues the following: The safe mode has prevented searchHostResult.php from accessing another directory. After turning the safe mode off it was possible to access ../database/DbHost.php but it was not possible to open this file.
The webserver has r-x - rights for accessing the files.
Is there any other option I have to set for accessing this php-files?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Include files

Post by BDKR »

Hola mi amigo,

Where is the file "DbHost.php" in relation to "searchHostResult.php"? What is the code you are using for your include statement? Is it something like

Code: Select all

include_once ("../database/DbHost.php");
Have you taken a look at the manual and read the section regarding "include_path"s? Also check what your $DOCUMENT_ROOT or $_SERVER[DOCUMENT_ROOT] var is set to.

Later on,
BDKR (TRC)
seem
Forum Newbie
Posts: 6
Joined: Fri Jun 28, 2002 9:48 am
Location: Munich, Germany

Post by seem »

Hello,

the warnings are caused by a stupid failure of me. searchHostsResult is included by index.php, which is stored in another folder. My include was in relation from searchHostResult to host.php but after changing it to the relation from index.php to hosts.php it worked fine.

Thanks for zou help :P
Post Reply