Define two functions or branch out inside one?

I read about how to determine the encoding of a file in PHP, and in some blog or somewhere, it was suggested to do this:

if (function_exists('mb_detect_encoding')) {
    function is_utf8($str) {
        // do stuff using the mb* libraries
    }
} else {
    function is_utf8($str) {
        // do stuff manually
    }
}

It seems very dirty to me and can be replaced with this:

function is_utf8($str) {
    if (...) {
        // mb stuff here
    } else {
        // manual stuff here
    }
}

However, I also see that it also has some advantages. Depending on how complex the operator ifis and how often the function is called, this can be much more efficient. My question is this: at what point could you consider dividing a function into two, as in the first example? Are there any other pros / cons that I missed?

Change . Please do not dwell on the example here, the question of this practice as a whole.

+3
6

, is_utf8. PHP , function_exists() .

+5

, , , OOP () factory.

class my_utf8 
{
  // A static method that determine what object to create
  public static function factory() {
    if (function_exists('mb_detect_encoding')) {
      return new my_utf8_with_mb;
    } else {
      return new my_utf8_without_mb;
    }
  }
}

class my_utf8_with_mb extends my_utf8 {
    public function is_utf8($str) {
        // do stuff using the mb* libraries
    }
}

class my_utf8_without_mb extends my_utf8 {
    public function is_utf8($str) {
        // do stuff without the mb* libraries
    }
}

:

global $myUtfInstance;
// Call once
$myUtfInstance = my_utf8::factory();

// later and forever...
$myUtfInstance->is_utf8($str);

singleton , . IF, .

class my_utf8 
{
  private static $instance;

  public static function factory() {
    if (function_exists('mb_detect_encoding')) {
      return new my_utf8_with_mb;
    } else {
      return new my_utf8_without_mb;
    }
  }


  public static function getInstance()
  {
    if (!self::$instance) {
      self::$instance = self::factory();
    }

    return self::$instance;
  } 
}

:

my_utf8::getInstance()->is_utf8($str);
+3

, . , .

    if (function_exists("sin")) {
        function baz($farfs) {
            print "I am the first baz";
        }
    } else {
        function baz($farfs) {
            print "I am the second baz";
        }
    }

    function blarg($fgar) {
        if (function_exists("sin")) {
            print "I am the first blarg";
        } else {
            print "I am the second blarg";
        }
    }

    for ($i=0;$i

, baz 50 75% blarg , .

:

  • function_exists ( "Foobar" )
    • blarg: 36,64
    • baz: 14.71
  • function_exists ( "" )
    • blarg: 35.24 ms
    • baz: 23.59 ms

- . , 10001 __ 0,18 0,11 . , - , .

, , . , , PHP, , , . , , PHP PHP .

shrug .

+2

. (, ) , .

+1

, , , PHP, - :

if (function_exists('mb_detect_encoding')) {
    function is_utf8_mb($str) {
        // do stuff using the mb* libraries
    }
}
function is_utf8_manual($str) {
    // do stuff manually
}

if (function_exists('is_utf8_mb')) {
    function is_utf8($str) {
        is_utf8_mb($str)
    }
} else {
    function is_utf8($str) {
        is_utf8_manual($str)
    }
}

: is_utf8 , , . , . , , , , . , , , , . , - , is_utf8 , .

0

: ( , ) .

: , .

, , . , , .

" ":

  • : ""
  • , .

That is why I would really create two “layers”: one layer sending the corresponding function, and another layer containing the “code blocks” wrapped in a function with its own name.

And then you have a choice whether to explicitly dispatch or use the PHP feature to declare functions on the fly.

// functionality: defines how to detect utf8 encoding 
//

function is_utf8_mb( $arg ) {
... // using the mb library
}

function is_utf8_bare( $arg ) {
... // hand coded
}

// dispatching layer: decide what library to use
//

// option 1: define on-the-fly
function define_utf8_function() {
   if( function_exists('mb_detect_encoding') ) {
     function is_utf8( $arg ) { return is_utf8_mb( $arg ); }
   } else {
     function is_utf8( $arg ) { return is_utf8_bare( $arg ); }
   }
}

// option 2: check the dispatching on each function call
function is_utf8_runtimedispatch( $arg ) {
  if( function_exists('mb_detect_encoding') ) 
    return is_utf8_mb( $arg );
  else 
    return is_utf8_bar( $arg );
}
0
source

Source: https://habr.com/ru/post/1725006/


All Articles