Why defining (global) constants in PHP is not recommended? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I heard so many times that use define() to create global constant is a bad solution. But I've never heard why it is so.
And what is the way it should be done instead?

Probably you've read something about not polluting the global namespace. At least that's the biggest reason why should you try to avoid global constants - and variables and functions for that matter. However there are valid use cases where a global definition makes sense.
In most cases though the constant belongs to something. That something being a class it's worth defining the constant within the class.
If you define a constant that has a long prefix (eg. VALIDATOR_EMAIL_PATTERN) that is a sign of a possible class related constant definition (Validator\Email::PATTERN)
Global definitions have more chance to collide thus making parts of your code harder to re use, since two different library could try to define constants with the same name for their our purpose.

Related

Laravel 5: constants.php vs constants in controller vs constants in model [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm using Laravel 5 and I need to add some immutable values (constants) to be able to make use of them in controllers mostly (maybe in views too). Question is: What's the best approach?
I've been reading and 90% of approaches suggest to use a constants.php and Config.get(), but I don't like this because (I think) a constant is not a config value. I mean, it's not supposed to be changed. In other frameworks, I like to use models or other lib class to define values related to the entity I'm working on, Eg.: I need a constant for cache time in users, then User::CACHE_1_DAY = '86400' (silly example btw).
I would recommend adding a new class containing Helper methods, and bind that class to the application in your AppServiceProvider.
Finally inside that class, place getter methods for the constants.

Can too many defined constants become counter productive? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is this a pitfall that some have encountered? I have only recently discovered the notion of defining a constant to use globally but find myself attempting to define away each and every repetitive block of code (no matter how long).
Can I get two major reasons when to avoid using constants and one supportive reason/example illustrating great use?
The reason not to use constants - Having global data is typically considered bad practice. Since PHP has the ability to be object oriented, most well written software takes advantage of it. Here is a pretty good explanation without getting too lengthy about encapsulation and abstraction.
The reasons I consider using them, typically for things that are reusable, that I would like to change all at once. an example i can think of is contact information. It's usually in the footer, sometimes the header and typically on a contact page. If I want to reuse a template I can just change the information in one spot and effectively update it on the entire site. Usually the database credentials live in the same file. I'm not saying this is the best practice, but it's convenient to only have to make changes to one file for global changes to a website.
That's just my 2 cents.

Is there any restriction in defining constants in PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Should I define constant and use it?
OR
Can I use Magic numbers?
I am working in project where I am using Magic number in many places do I want to change it? because I am using more that 1000 Magic number? Is this rite way or I should use constant?
Is there any restriction in the number of constants?
Ex:
Magic numbers:
$common->getDBMessage(25);
Constants:
$common->getDBMessage(ERROR_MESSAGE_INVALID_PHONE);
You can have as many constants as you like. PHP already has a very large number of constants built in.
You might want to consider making them class constants (const within a class) rather than global constants (using define). Having them organised will help you structure your code better and make things easier to work with.
In addition, if some of the constants are related, you might consider using bit-masks for groups of values, so you can specify them together. This will avoid the need to create an additional constant for the combined value.
But all this is just advice for making your life easier; it's not related to whether you can do it or not.
There are no restrictions for constants in PHP - at least, no native restrictions.
It's a matter of good practice - if you'll have 1000 constants with bad naming, this will be difficult to understand by those who will read your code. And - I do not understand why use constants for numbers unless they have model-defined (or application-defined) meaning.
I.e. if you have some service 'FOO' with id=1, then SERVICE_FOO_ID constant with value 1 is normal solution, but if you want to create constant with value 1 for case for($i=1; ...) - that's sounds weird.
The common answer will be, of cause: it's opinion-bases. It's up to you.

My colleague writes code on PHP, and uses as a constant ... attention - static methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I.e. instead of:
const MY_CONST = 0;
he writes:
public static function MY_CONST() { return 0 };
My former colleagues, front-end developers, doing so in JavaScript, because there are no constants, but to do so in PHP? In my opinion, if the language have a constants, you should use them.
I want to send him a link to this question, as my arguments.
Writing method to return always same value is senseless. Of course it is better pratice to use constans. Even when you look at perfomace it's faster when you declare something that you know won't change as constant.
I agree with hakre don't argue with idiots they will bring you to their level and beat with experience.
I read somewhere that in PHP constans are not so fast. You should read this article.
http://planetozh.com/blog/2006/06/php-variables-vs-constants/
In my opinion, if the language have a constants, you should use them.
Sure, that's why they are in. Otherwise it is not a constant. Did he said he needs a constant? If so, tell him there is a PHP manual explaining it in case the language is new to him.
The URLs are http://php.net/const and http://php.net/constants .
Apart from these fundamental and bare things, do not argue with idiots. They live - by the original meaning of the word - in their own world. So you can not get through to them.

Using Java-Style Namespace Convention in PHP or is it better to use X? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I know that Namespaces in PHP are relatively new and so there are many possibilities to use them. Because it was not usable on the Server i did define my classnames like folder_folder_classname if they where in /folder/folder/classname.php
Now i want to change that to get better class names. I thought to redesign my class structure to a java based one, like com\domain\project\folder\folder\class (e.g. in java: com.domain.project.folder.folder package structure) and was thinking that this is a nice solution to avoid any problems with other classes i ever use and don't think about include() for my classes any more :)
So now my Questions:
Is this a good idea or should I use a better system for my php classes?
Is there a common handling for that in php?
(I saw that Zend Framework uses e.g. instead of Zend_Controller_Request_Abstract the namespace Zend\Controller\Request with class name RequestAbstract -> no domain and double wording...)
thanks for your answers :)
I would definitely avoid using the domain wording and just prepend the project name itself like Zend does (that way you avoid collision, too). Since you are thinking about restructuring and if you are using PHP > 5.3.0 (which you should) you might also want to consider the PHP namespaces.

Categories