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 6937285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:20:36+00:00 2026-05-27T12:20:36+00:00

I am trying to refer to a defined constant in a separate function. The

  • 0

I am trying to refer to a defined constant in a separate function.
The errors I am getting refer to the variable not defined and the variable defined as a constant for each FOO and BAR.

class Boo {
public function runScare() {
    $this->nowScaring('FOO');
    $this->nowScaring('BAR');
}
private function nowScaring($personRequest) {
    define ('FOO', "test foo");
    define ('BAR', "test bar");
    $person = ${$personRequest};
    echo "<br/>Query selected is: " . $person . "<br/>";
}
}
$scare = new Boo;
$scare->runScare();
  • 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-27T12:20:37+00:00Added an answer on May 27, 2026 at 12:20 pm

    Constants should be defined once only, at the top of your script, like this:

    define ('FOO', "test foo"); 
    define ('BAR', "test bar"); 
    

    Then, to access them, don’t put their names in quotes:

    class Boo { 
      public function runScare() { 
          $this->nowScaring(FOO); // no quotes
          $this->nowScaring(BAR); // no quotes
      } 
      private function nowScaring($person) {
          // And no need to "grab their values" -- this has already happened
          echo "<br/>Query selected is: " . $person . "<br/>"; 
      } 
    } 
    

    If for some reason you want to get the value of the constant and all you have is its name in a variable, you can do so with the constant function:

    define ('FOO', "test foo"); 
    
    $name = 'FOO';
    $value = constant($name);
    
    // You would get the same effect with
    // $value = FOO;
    

    In this particular case, it looks like that class constants might be a better fit:

    class Boo { 
      const FOO = "test foo";
      const BAR = "test bar";
    
    
      public function runScare() { 
          $this->nowScaring(self::FOO); // change of syntax
          $this->nowScaring(self::BAR); // no quotes
      } 
      private function nowScaring($person) {
          echo "<br/>Query selected is: " . $person . "<br/>"; 
      } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to properly hook/detour a virtual function in a class object, and
I am trying to write a recursive function within a class, but have some
I'm trying to refer to a drive whose letter may change. I'd like to
I am trying to refer to a location on my computer, however Java is
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
How can I make a ForeignKey refer back to the object itself? I'm trying
I'm trying to determine a subset of nodes stored in the variable objs by
Here's a very simple example of what I'm trying to get around: class Test(object):
I've been trying to pickle an object which contains references to static class methods.
I have an Android widget (extends the AppWidgetProvider class) and I'm trying to access

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.