sennetta Posted November 26, 2007 Share Posted November 26, 2007 I'm making use of inheritance capabilities offered in PHP5. I'm including the parent class (using the php require() function) at the head of the child class, and then going on to write the child class, and saving all classes as .php files. Firstly, is this the standard practice in php? I'm used to Java importing packages rather than including files... Also this is causing problems when the classes are accessed from different directories. I have a site and there's an admin directory (www.mysite.com/admin) and a lib directory which contains all my classes (www.mysite.com/lib). From within the admin dir, I use a class "Bar" from by including "Bar.php": <?php include("../lib/Bar.php"); $barObj = new Bar(); // continue with proceedual, html-output type stuff here..... ?> Bar itself inherits from a class called "Foo" which also resides in the lib directory. So, at the top of "Bar": <?php include("Foo.php"); class Bar extends Foo { // class content } ?> The trouble is there is a file called "foo.php" (with a small letter, as it isn't a class) in the admin directory, and the include function in the Bar class is including both these files even though the file-names are different. It says in the documentation (http://uk2.php.net/include/) that the file is looked for "in the current working directory" (in my case "/admin"?), then in the directory of the current script (presumably "/lib"?), and so both the files are being included, despite me explicit asking for "Foo.php" with a capital letter. I only want what I ask for to be included, not this random page which happens to have a similar (not identical) file name. Are there any workarounds for this any one can suggest? Many thanks, Anthony Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 26, 2007 Share Posted November 26, 2007 Maybe you missed it, but there's a sub-forum under this one called "OOP". = ) You might want to post over there... PhREEEk Quote Link to comment Share on other sites More sharing options...
sennetta Posted November 26, 2007 Author Share Posted November 26, 2007 I thought about that, but this isn't really an OOP problem - it's just these nested includes I'm having problems with... Quote Link to comment Share on other sites More sharing options...
trq Posted November 26, 2007 Share Posted November 26, 2007 No its not really an OOP problem. Include files should be included relative to the original page being called. This may be causing your issue. I always use full paths for my includes. You might also take a look at using the __autoload function. Used in conjunction with set_include_path can also help. PS: If you have classes including classes all over the place too, its probably safest to use include_once. Even if you include a file within a classes definition they enter the global scope. Quote Link to comment Share on other sites More sharing options...
sennetta Posted November 26, 2007 Author Share Posted November 26, 2007 Have to head off but I will try what you said. Nice one. Anthony Quote Link to comment Share on other sites More sharing options...
sennetta Posted November 28, 2007 Author Share Posted November 28, 2007 Cheers thorpe. Works like a dream. The manual implies that this should be used as a last resort, but I'm thinking of just using it for all my classes. Is this advisable? Quote Link to comment Share on other sites More sharing options...
trq Posted November 28, 2007 Share Posted November 28, 2007 What the __autoload functiion? I use it pretty well exclusively, though I may hard code classes that I know will be required on every request. Theres some cool tricks can be done with the __autload function too. You can almost get the impression of having namspaces, well, not really but you can organise things allot better. Say you have a file structure. [pre] / index.php inc/ db/ foo.class.php util/ foo.class.php [/pre] db/foo.class.php <?php class db_foo {} ?> util/foo.class.php <?php class util_foo {} ?> index.php <?php function __autoload($class) { $class = str_replace('_','/',$class); if (file_exists('inc/' . $class . '.class.php'); require_once 'inc/' . $class . '.class.php'; } } $foo = new db_foo; ?> Theres other tricks too, like making it look through a specific set of paths until it finds a matching class etc etc. Anyway, yeah, it comes in real handy. Quote Link to comment Share on other sites More sharing options...
sennetta Posted November 28, 2007 Author Share Posted November 28, 2007 Ah nice one. Would never have thought of doing that! I'm nowhere near the stage of having massive great numbers of files at the moment (I'm a novice) but as I delve deeper I shall bear that in mind. Many thanks for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.