Jump to content

Undefined Constant


The Little Guy

Recommended Posts

I have a class, and some of the methods take parameters like this: EXTRACT_SYMBOL, EXTRACT_NUMBER, etc. when I run this, I get the following:

Notice: Use of undefined constant EXTRACT_SYMBOL - assumed 'EXTRACT_SYMBOL' in C:\wamp\www\phpLive\index.php on line 11

 

What should I do, not to turn the errors off, but to make the code valid? What ever I need to do, I want to do within the class/method, and not outside it if possible.

 

public class myClass{
    public function myFunction($value, $action){
        switch($action){
             case EXTRACT_SYMBOL:
                 // Do some stuff
                 break;
             case EXTRACT_NUMBER:
                 // Do some stuff
                 break;
        }
    }
}

$cls = new myClass();
$cls->myFunction('Some string', EXTRACT_NUMBER);

 

Thanks!

Link to comment
Share on other sites

looks like I want this

define('EXTRACT_SYMBOL',1); // a constant

 

Do I put that above my class?

 

Like this:

define('EXTRACT_SYMBOL',1);
public class myClass{
    public function myFunction($value, $action){
        switch($action){
             case EXTRACT_SYMBOL:
                 // Do some stuff
                 break;
             case EXTRACT_NUMBER:
                 // Do some stuff
                 break;
        }
    }
}

$cls = new myClass();
$cls->myFunction('Some string', EXTRACT_NUMBER);

 

or inside the constructor?

Link to comment
Share on other sites

If using classes, you should define them as a class constant:

 

class Foo
{
    const constant1 = 1;
    const constant2 = 2;

    [...]

 

Then accessed through:

 

self::constant1 // from within the class
Foo::constant1 // from outside of the class

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.