我正在为文件中存在的每个刺创建常量,所以它是好主意还是坏主意?以后当某些东西被破坏时,跟踪是容易还是困难?
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);
}
}
请建议我。
<?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();
运行此代码 3 次后,与带有字符串的样式进行比较并使用常量查看执行时间如下。
常量 = 0.90771412849426字符串 = 0.74732899665833
常量 = 0.94015312194824字符串 = 0.7591450214386
常量 = 0.89980792999268字符串 = 0.79145216941833
请参考此链接,可能会对您获得答案有用......
PHP 常量:优点/缺点
http://amityug.org/wordpress/zimlich/2014/12/28/php-constants-advantagesdisadvantages/
http://imrannazar.com/Memory-Usage-of-Constants-in-PHP