note 100142 added to language.oop5.constants

From: Date: Tue, 28 Sep 2010 01:32:19 +0000
Subject: note 100142 added to language.oop5.constants
Groups: php.notes 
Request: Send a blank email to php-notes+get-172755@lists.php.net to get a copy of this message
Most people miss the point in declaring constants and confuse then things by trying to declare things like functions or arrays as constants. What happens next is to try things that are more complicated then necessary and sometimes lead to bad coding practices. Let me explain... A constant is a name for a value (but it's NOT a variable), that usually will be replaced in the code while it gets COMPILED and NOT at runtime. So returned values from functions can't be used, because they will return a value only at runtime. Arrays can't be used, because they are data structures that exist at runtime. One main purpose of declaring a constant is usually using a value in your code, that you can replace easily in one place without looking for all the occurences. Another is, to avoid mistakes. Think about some examples written by some before me: 1. const MY_ARR = "return array(\"A\", \"B\", \"C\", \"D\");"; It was said, this would declare an array that can be used with eval. WRONG! This is just a string as constant, NOT an array. Does it make sense if it would be possible to declare an array as constant? Probably not. Instead declare the values of the array as constants and make an array variable. 2. const magic_quotes = (bool)get_magic_quotes_gpc(); This can't work, of course. And it doesn't make sense either. The function already returns the value, there is no purpose in declaring a constant for the same thing. 3. Someone spoke about "dynamic" assignments to constants. What? There are no dynamic assignments to constants, runtime assignments work _only_ with variables. Let's take the proposed example: /** * Constants that deal only with the database */ class DbConstant extends aClassConstant { protected $host = 'localhost'; protected $user = 'user'; protected $password = 'pass'; protected $database = 'db'; protected $time; function __construct() { $this->time = time() + 1; // dynamic assignment } } Those aren't constants, those are properties of the class. Something like "this->time = time()" would even totally defy the purpose of a constant. Constants are supposed to be just that, constant values, on every execution. They are not supposed to change every time a script runs or a class is instantiated. Conclusion: Don't try to reinvent constants as variables. If constants don't work, just use variables. Then you don't need to reinvent methods to achieve things for what is already there. ---- Server IP: 69.147.83.197 Probable Submitter: 93.195.79.40 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php Edit -- https://master.php.net/note/edit/100142 Del: integrated -- https://master.php.net/note/delete/100142/integrated Del: useless -- https://master.php.net/note/delete/100142/useless Del: bad code -- https://master.php.net/note/delete/100142/bad+code Del: spam -- https://master.php.net/note/delete/100142/spam Del: non-english -- https://master.php.net/note/delete/100142/non-english Del: in docs -- https://master.php.net/note/delete/100142/in+docs Del: other reasons-- https://master.php.net/note/delete/100142 Reject -- https://master.php.net/note/reject/100142 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172755) next »