Forums / Templates, Stylesheets, Page Layout / Correct method to add new define constant?

Correct method to add new define constant?

Locked

Views: 2,584

Results 1 to 5 of 5
This thread is locked. New replies are disabled.
7 May 2010, 5:22 AM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Correct method to add new define constant?

If I want to use a new constant, for example in the moneyorder payment method,
I could
a) Add this constant to the** original** defines file
b) Create an override file and add the constant there only
c) Create an override file and add the constant there and in the original file
d) Add it to a file in the extra_defines folder.

These all seems to work correctly and previously I have always done b) but I suspect that is not correct as I have recently seen php error notices that indicate it cannot actually "see" that constant but assumes it (usually correctly).

Would I be correct in saying the overrides file should only be used for overriding the original constants defined in the original language file and so d) is the correct option?

thanks
Steve

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

7 May 2010, 2:10 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Correct method to add new define constant?

Could you expand on what you mean by:

These all seems to work correctly and previously I have always done b) but I suspect that is not correct as I have recently seen php error notices that indicate it cannot actually "see" that constant but assumes it (usually correctly).

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

7 May 2010, 5:10 PM
#3
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Correct method to add new define constant?

Well, while upgrading and cleaning up/fixing buglets I had the php error reporting set to -1/E_ALL.
It generated a lot of notices referring to undefined variables and undefined and duplicated constants, most of which referred to stuff I had done, so I went about fixing them.

I can only give you this example as I have fixed all the rest:

[07-May-2010 20:49:12] PHP Notice: Use of undefined constant TEXT_NO_PRODUCTS - assumed 'TEXT_NO_PRODUCTS' in D:\My Documents.....139b\includes\modules\CUSTOM\product_listing.php on line 315

(This specific error will not appear on a default installation, it is due to a mod).

All were due to the fact that I had added a new constant into an override file but not the original file. So I decided to put all new constants in one file in /extra_defines and only use the override files to redefine original constants. This fixed all the notices regarding undefined constants.

While now it seems sort of obvious that new stuff should go in extra_defines, it also seemed logical many moons ago when I first set foot on the Zen road that they should go in the overrides file. Looking for such information now yields nothing written about it, hence my question to get something in print about it.

At no point prior to this were there any real warnings or errors displayed, php always assumed correctly to use the new define in the override, but we all want an error-free shop don’t we, even the ones we can't see!

Steve

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

7 May 2010, 6:00 PM
#4
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Correct method to add new define constant?

The answer is ... it depends.

Some language overrides work by first loading the override file and then the original to plug any gaps, others by loading the override file in place of the original.

For added confusion, there has been some shifting (for the better) between these approaches over the course of the 1.3 releases.

Adding them in the original file only risks the addition being lost during an upgrade, and even if a file comparison is done, it still makes upgrades a bit harder as it mixes changes with core code and increases the number of changes that need to be analysed so a is not a good idea.

Where files are overloaded it's only necessary to add new or changed terms. The best example of this are the main language files e.g. english.php (option b).

When they're not overloaded just replaced (most common), you need to copy the entire file to the override folder and make any changes or additions there. This is the best option (c) when a defined constant is need only in one place or on one page and an override mechanism exists. That's not the case for the Admin, but I believe it is for payment modules.

By extra_defines (d) I assume you mean extra_definitions (which in turn is different from extra_datafiles, which also contains define statements). These files are loaded for every page. There not strictly overrides but rather a different loading mechanism. Defines put in here will be loaded on every page, so it should really be used only if it is widely needed and doesn't clash with anything defined elsewhere.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

7 May 2010, 6:21 PM
#5
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Correct method to add new define constant?

Ha! I knew it wasn't straighforward.

I think that goes some way to explaining why there are also a lot of notices generated in respect to constants defined more than once (usually in english.php and then somewhere else).

Thanks for taking the time to write such a detailed description,

Steve

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...