0

When I initialize a new class instance I want to set a public variable based on the current url.

How do I go about setting the public variable dynamically when the class instance is created so it is available without having to call a function.

 class CONFIGURATOR{
    static public $ACTIVE = true;
    public $CURRENT_URL="<current_url here>"
  }
1
  • You are unclear with your question, but you can either populate that variable via constructor when you're creating the object or after you created the object via $obj->CURRENT_URL = 'your url'; Commented May 15, 2013 at 8:56

2 Answers 2

2

Use the constructor :

class CONFIGURATOR{
   static public $ACTIVE = true;
   public $CURRENT_URL="<current_url here>"

   public function __construct($url)
   {
       $this->CURRENT_URL = $url;
   }
}

You can call your object like this :

$configurator = new CONFIGURATOR($your_url);
Sign up to request clarification or add additional context in comments.

Comments

0

Use constructor , it may be solve your problem

class CONFIGURATOR{
    static public $ACTIVE = true;
    public $CURRENT_URL="";

    function __construct()
    {
        $this->$CURRENT_URL="<current_url here>";
    }
  }

Thank You

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.