Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/266746-class-constants-scope/
Share on other sites

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)

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->?

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

 

 

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.

 

 

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.

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.