timothyneill Posted August 6, 2012 Share Posted August 6, 2012 I am returning to PHP after a period of absence. I am having problems with using class constants within another class's constructor: The class HttpKeys contains all the keys the site uses for the $_POST array. final class HttpKeys { const EMPLOYEE_HTTP = 'employeeHttpKey'; const EMPLOYEE_LOG_OUT = 'employeeLogOutKey'; const EMPLOYEES_UPDATED = "employeesUpdated"; const LOCKS_UPDATED = "locksUpdated"; const MENU_ITEM_COUNTS_UPDATED = "menuItemCountsUpdated"; const MENU_ITEMS_UPDATED = "menuItemsUpdated"; const MODIFIERS_UPDATED = "modifiersUpdated"; const ORDERS_UPDATED = "ordersUpdated"; const PAYLOAD = "payload"; const PRODUCT_CATEGORIES_UPDATED = "productCategoriesUpdated"; const STATUS = "status"; const TERMINAL_HTTP = 'terminalHttpKey'; const TERMINAL_LOG_OUT = 'terminalLogOutKey'; const TERMINAL_REGISTRATION = 'terminalRegistrationKey'; const TIMESTAMP = "now"; } However, These class constants are not always available in the following code. Is this by design or a bug? require('HttpKeys.php'); class ClientUpdates { function __construct() { //-- this does not work $i = HttpKeys::PRODUCT_CATEGORIES_UPDATED; $this->getProductCategories(); } private function getProductCategories() { //-- this does not work $j = HttpKeys::PRODUCT_CATEGORIES_UPDATED; } } //-- this works $k = HttpKeys::PRODUCT_CATEGORIES_UPDATED; $clientUpdates = new ClientUpdates(); $clientUpdates = null; unset($clientUpdates); Thanks everyone for all the help - apologies for being a newbie once again. Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/ Share on other sites More sharing options...
requinix Posted August 6, 2012 Share Posted August 6, 2012 What do you mean by "does not work"? Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367350 Share on other sites More sharing options...
timothyneill Posted August 6, 2012 Author Share Posted August 6, 2012 HttpKeys::PRODUCT_CATEGORIES_UPDATED returns null where marked //-- this does not work Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367351 Share on other sites More sharing options...
requinix Posted August 6, 2012 Share Posted August 6, 2012 So you did a var_dump(HttpKeys::PRODUCT_CATEGORIES_UPDATED); and got NULL If not then what did you do? You wouldn't happen to be trying to use member variables and forgetting to include the $this->? Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367354 Share on other sites More sharing options...
btherl Posted August 6, 2012 Share Posted August 6, 2012 I copied and pasted your class definitions, added var_dump() and created an instance. The code is: <? final class HttpKeys { const EMPLOYEE_HTTP = 'employeeHttpKey'; const EMPLOYEE_LOG_OUT = 'employeeLogOutKey'; const EMPLOYEES_UPDATED = "employeesUpdated"; const LOCKS_UPDATED = "locksUpdated"; const MENU_ITEM_COUNTS_UPDATED = "menuItemCountsUpdated"; const MENU_ITEMS_UPDATED = "menuItemsUpdated"; const MODIFIERS_UPDATED = "modifiersUpdated"; const ORDERS_UPDATED = "ordersUpdated"; const PAYLOAD = "payload"; const PRODUCT_CATEGORIES_UPDATED = "productCategoriesUpdated"; const STATUS = "status"; const TERMINAL_HTTP = 'terminalHttpKey'; const TERMINAL_LOG_OUT = 'terminalLogOutKey'; const TERMINAL_REGISTRATION = 'terminalRegistrationKey'; const TIMESTAMP = "now"; } class ClientUpdates { function __construct() { //-- this does not work $i = HttpKeys::PRODUCT_CATEGORIES_UPDATED; var_dump($i); $this->getProductCategories(); } private function getProductCategories() { //-- this does not work $j = HttpKeys::PRODUCT_CATEGORIES_UPDATED; var_dump($j); } } $c = new ClientUpdates; ?> And the output is string(24) "productCategoriesUpdated" string(24) "productCategoriesUpdated" So it is working here. My php version is PHP 5.3.3-7+squeeze13 with Suhosin-Patch (cli) (built: Jun 10 2012 07:31:32) Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367356 Share on other sites More sharing options...
timothyneill Posted August 6, 2012 Author Share Posted August 6, 2012 This end: PHP Version 5.3.2-1ubuntu4.17 Could be a bug with this particular version - thanks for all the help Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367362 Share on other sites More sharing options...
requinix Posted August 7, 2012 Share Posted August 7, 2012 Could be a bug with this particular version Not to be dismissive but no. So you did a var_dump(HttpKeys::PRODUCT_CATEGORIES_UPDATED); and got NULL If not then what did you do? You wouldn't happen to be trying to use member variables and forgetting to include the $this->? Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367365 Share on other sites More sharing options...
timothyneill Posted August 7, 2012 Author Share Posted August 7, 2012 I could be wasting your time - I just did a var_dump and the results were as they should be. I may have a problem with XDebug with PDT in eclipse. Strangely in the debugger the var_dump is working - however assignment to $i & $j do not. I'm so sorry to be such a noob Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367372 Share on other sites More sharing options...
btherl Posted August 7, 2012 Share Posted August 7, 2012 Can you post the code which doesn't work? The code from the original post doesn't use $i $j or $k so you can't tell from the behaviour whether the assignments work or not. Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367381 Share on other sites More sharing options...
timothyneill Posted August 7, 2012 Author Share Posted August 7, 2012 The code from the first post does include $i & $j - however it is in the ClientUpdates php code block. The more I play with this - I have it working using var dumps - however my step through debugger (XDebug) was not showing the vars being assigned values. This could be an issue with my config of PDT & Xdebug. Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367385 Share on other sites More sharing options...
requinix Posted August 7, 2012 Share Posted August 7, 2012 Please, please just post the code you're using. Without paraphrasing or generalizing. It'll make things a lot easier for us to see. Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367386 Share on other sites More sharing options...
timothyneill Posted August 7, 2012 Author Share Posted August 7, 2012 Here is the code: 18795_.php 18796_.php 18797_.php Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367387 Share on other sites More sharing options...
btherl Posted August 7, 2012 Share Posted August 7, 2012 The code from the first post does include $i & $j - however it is in the ClientUpdates php code block. The more I play with this - I have it working using var dumps - however my step through debugger (XDebug) was not showing the vars being assigned values. This could be an issue with my config of PDT & Xdebug. So is the situation that var_dump($i) shows the expected value but XDebug doesn't show it? I strongly suspect it's an issue with XDebug then. If var_dump() works then you are getting the correct value, and your script should be able to run as intended. Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367399 Share on other sites More sharing options...
timothyneill Posted August 7, 2012 Author Share Posted August 7, 2012 I agree - I have just upgraded eclipse to juno. There could be a problem there. I am very sorry to have consumed a bunch of your time on a problem that isn't a language issue. I may go back to indigo and check it out. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/266746-class-constants-scope/#findComment-1367472 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.