How do I set up this construct correctly?

I've got a page "view_class.php." On that page I have several classes and functions which I call from my index page like this:


Notice that the page resides in a different directory other than the one my current page resides in.

At one point, I've got a function on my view_class.php page that's looking for a file in the "Photos" directory (<a href="Photos/"). Because the page that's calling this function resides in something other than the root folder, I'm getting an error that says the file doesn't exist.

What I want to do (I think) is set up a construct on my view_class.php page that defines the root directory. Something like this:

function rootDirectory()
            if (strpos($root_directory,'wamp') !== false)
                  $basic = str_replace("C:\wamp\www\\", "http://localhost\\", "$root_directory");
            $working_root = "http://";
            $working_root.= $_SERVER['SERVER_NAME'].'/';      

      return $working_root;

...and then when I got to run my function on my view_class.php, instead of <a href="Photos", I'll do <a href="'.rootDirectory().'/Photos".

I've done this previously on another page on the fly, but I want to learn how I can establish a variable in the context of a construct so sitting at the top of my view_class.php page, I've got something like the function I have above and then when I call another function within that same page, it's grabbing that variable that was established within the construct.

I hope that makes sense. So, provided I'm barking up the right tree, how do I do that?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
If I understand the question correctly, there are known design patterns for this sort of thing, used almost universally in all PHP packages.  I don't have a good example for you at my fingertips, and I have to leave for work now, but please take a look at the SPL Autoloader.  It's how these problems are solved today.  Absolute file paths are usually defined constants in an app config file.  These links should get you started...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Please forgive me if I point out something too basic, but I just wanted to make sure you don't spend time on making things too complicated.

When your index.php-file includes the view_class.php-file any code within the included file will run as if you copied the entire content of view_class.php and pasted it in your index.php-file at the same location as "require_once" in index.php

So if you have this file structure:


If you intend to include view_class.php in index.php, then you must refer to birds.jpg as "../photos/birds.jpg", not "/photos/birds.jpg". Meaning: The path must be relative to where the code will be run (index.php) not to where the file with the code is stored (view_class.php).

All the above is relevant if you want to use relative paths.

If you want to use absoulte paths these will be the same whereever you are in your website. So if your website is: "" and your Photos-folder is "", then your function does not need to be more complicated than this:

function mybaseurl() {

And it can be used like you suggested:
<a href="'.mybaseurl().'Photos">.

An even simpler solution could be to make a configuration file:
   $mybaseurl = "";

And then include this one in your files:

Then  you can use it like this:
     <a href="'.$mybaseurl.'Photos">.
Bruce GustPHP DeveloperAuthor Commented:
Thank you, gentlemen! I was able to figure it out!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.