PHP class 中每个字符串的常量都存在于class 中,是好是坏?
PHP class constant for each string exist in class, Is good or bad idea?
我正在为文件中存在的每个字符串创建常量,所以这是好主意还是坏主意?
稍后当出现问题时,跟踪是容易还是困难?
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);
}
}
请推荐我。
请参考这个link可能对你得到答案有用......
PHP Constants: Advantages/Disadvantages
http://amityug.org/wordpress/zimlich/2014/12/28/php-constants-advantagesdisadvantages/
<?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
我正在为文件中存在的每个字符串创建常量,所以这是好主意还是坏主意? 稍后当出现问题时,跟踪是容易还是困难?
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);
}
}
请推荐我。
请参考这个link可能对你得到答案有用......
PHP Constants: Advantages/Disadvantages
http://amityug.org/wordpress/zimlich/2014/12/28/php-constants-advantagesdisadvantages/
<?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