简体   繁体   中英

PHP class constant for each string exist in class, Is good or bad idea?

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.

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM