The Little Guy Posted July 11, 2011 Share Posted July 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/ Share on other sites More sharing options...
gristoi Posted July 11, 2011 Share Posted July 11, 2011 where have you declared the constant for EXTRACT_NUMBER ? Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241317 Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2011 Share Posted July 11, 2011 Do you want those to be defined constants or literal strings - define('EXTRACT_SYMBOL',1); // a constant or Strings - case 'EXTRACT_NUMBER': $cls->myFunction('Some string', 'EXTRACT_NUMBER'); Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241319 Share on other sites More sharing options...
The Little Guy Posted July 11, 2011 Author Share Posted July 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241321 Share on other sites More sharing options...
Adam Posted July 11, 2011 Share Posted July 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241335 Share on other sites More sharing options...
The Little Guy Posted July 11, 2011 Author Share Posted July 11, 2011 but they are being passed as parameters to the methods Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241354 Share on other sites More sharing options...
Adam Posted July 11, 2011 Share Posted July 11, 2011 Yeah..? If outside of the class, use Foo::constant: $foo->someMethod(Foo::constant) IF you're calling the method internally, use self. Quote Link to comment https://forums.phpfreaks.com/topic/241693-undefined-constant/#findComment-1241359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.