Reputation: 83
I am creating const for every sting exist in file so its good or bad idea? later when something is break than tracing will easy or hard?
class GoogleSpreadsheet {
const WHITE_SPACE=' ';
const TIME_LIMIT=0;
const REDIRECT_PATH='test.php';
const FLAG_ONE=1;
const SET_MEMORY_LIMIT_VALUE=-1;
const FIRST_ELEMENT_INDEX=0;
const SUBTRACT_CONDITION='-1';
const MAX_NEW_WORKSHEET_ROWS=10;
const ALPHANUMERIC_CHARACTERS_REGEX='/[^A-Za-z0-9\-\.]+/';
const FORWARD_SLASH_SEPARATORS="/";
const URL_SELF_FIELD='self';
const SET_MEMORY_LIMIT='memory_limit';
function __construct() {
set_time_limit(self::TIME_LIMIT);
ini_set(self::SET_MEMORY_LIMIT, self::SET_MEMORY_LIMIT_VALUE);
extract(func_get_arg(self::FIRST_ELEMENT_INDEX));
$this->spreadsheetKey=(isset($spreadsheetKey))?$spreadsheetKey:$this->spreadsheetKey;
$this->worksheetId=(isset($worksheetId))?$worksheetId:$this->worksheetId;
if (isset($googleUsername)&&isset($googlePassword)) {
$this->loginGoogle($googleUsername, $googlePassword);
}
}
Please suggest me.
Upvotes: 0
Views: 153
Reputation: 245
<?php
Class constantTest{
const MY_VALUE_TEST='hello Word';
function constantTest(){
$startTime=microtime(true);
$string='';
for($i=0;$i<1000000;$i++){
$string.=self::MY_VALUE_TEST;
}
$endTime=microtime(true);
$totalTime=$endTime-$startTime;
echo 'Constant = '.$totalTime;
}
}
new constantTest();
echo "<br>";
Class StringTest{
function StringTest(){
$startTime=microtime(true);
$string='';
for($i=0;$i<1000000;$i++){
$string.='hello Word';
}
$endTime=microtime(true);
$totalTime=$endTime-$startTime;
echo 'String = '.$totalTime;
}
}
new StringTest();
After running this code 3 times ,After comparison with both the style with string and using constant review the execution time as below.
Constant = 0.90771412849426 String = 0.74732899665833
Constant = 0.94015312194824 String = 0.7591450214386
Constant = 0.89980792999268 String = 0.79145216941833
Upvotes: 4
Reputation: 245
Please refer this link may be it will useful for you to get an answer......
PHP Constants: Advantages/Disadvantages
http://amityug.org/wordpress/zimlich/2014/12/28/php-constants-advantagesdisadvantages/
http://imrannazar.com/Memory-Usage-of-Constants-in-PHP
Upvotes: 3