Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7167071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:31:04+00:00 2026-05-28T14:31:04+00:00

What is the best way to make a variable accessable to all classes. For

  • 0

What is the best way to make a variable accessable to all classes.

For example, I want to have a configuration file (Call it config.php) that is going to have a variable like so:

$server_url = "www.myaddress.com";

And I have a main library type file that contains a bunch of classes that need to access the $server_url. So here begins that main library file:

require 'config.php';

class one {
  function a() {
          $html = "<a href='$server_url/main.php'>LINK</a>"
         return $html;
         } 

  function b() {
        $html = "<a href='$server_url/about.php'>LINK</a>"
        return $html;

         }
}


class two {

    function new() {
          $html = "<a href='$server_url/blah.php'>LINK</a>

     } 
}

What would be the best way to make $server_url from the config.php available to every function? Or at least available to all the functions in a class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:31:04+00:00Added an answer on May 28, 2026 at 2:31 pm

    Personally I would use a static entity to hold all configuration values.

    Usually, most php applications have a single entry point (index.php) that can load up the config.php file and make the static entity available from that point.

    If your application has multiple entry points, then you will need to include config.php in each of these points.

    Something like this:

    <?php
        class Configurator{
            private static $_configuration = array();
            public static function write($key, $value) {
                self::$_configuration[$key] = $value;
            }
            public static function read($key) {
                return self::$_configuration[$key];
            }
        }
        Configurator::write('server', 'http://localhost');
        Configurator::read('server');
     ?>
    

    CakePHP has a similar class: http://api.cakephp.org/view_source/configure/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way (without js) to make all cells align (ie, have
What is the best way to make sure that all the environment variables I
What is the best way to make a variable that works exactly like a
What is the best way to make a Delphi application (Delphi 2007 for Win32)
What is the best way to make a <div> fade away after a given
What is the best way to make trailing slashes not matter in the latest
What is the best way to make one of the branches of a subversion
what's the best way to make communications between page widgets residing on differents ViewPart?
What is the best way to make an HTTP GET request in Ruby with
What is the best way to make my Qt Ruby application into an executable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.