Welcome Guest, Not a member yet? Register   Sign In
Suggestion: Class to handle Strings
#5

(This post was last modified: 09-10-2015, 03:47 AM by sv3tli0.)

Here is an example:


PHP Code:
<?php


class StringLib
{
 
   // This should be Container not library variable..
 
   // But for tests is fine..
 
   static $lib;

 
   public function __construct($lib)
 
   {
 
       return $this->testChangeLib($lib);
 
   }

 
   // Method just for tests
 
   public static function testChangeLibStatic$lib )
 
   {
 
       $className $lib get_called_class();
 
       ifclass_exists$className ))
 
       {
 
           static::$lib = new $className;
 
       }
 
   }

 
   // Method just for tests
 
   public function testChangeLib$lib ) : StringLib
    
{
 
       $className $lib get_called_class();
 
       ifclass_exists$className ))
 
       {
 
           static::$lib = new $className;
 
           return $this;
 
       }
 
   }


 
   public function __callstring $method, array $args = [])
 
   {
 
       ifmethod_exists(static::$lib$method))
 
       {
 
           return call_user_func_array( array(static::$lib$method), $args);
 
       }
 
   }

 
   public static function __callStaticstring $method, array $args  = [])
 
   {
 
       ifmethod_exists(static::$lib$method))
 
       {
 
           return call_user_func_array(array(static::$lib$method), $args);
 
       }
 
   }
}

interface 
StringInterface
{
 
   public function strlenstring $string) : int;
}



class 
PHPStringLib implements StringInterface
{
 
   public function strlenstring $string) : int
    
{
 
       return strlen($string);
 
   }
}


class 
MBStringLib implements StringInterface
{
    
// This File may have error trigger if MB is disabled
 
   // This should be configurable
 
   static $charset 'UTF-8';

 
   public function strlenstring $string) : int
    
{
 
       return mb_strlen$stringself::$charset);
 
   }
}


class 
CustomStringLib
{
 
   public function strlen($string) : int
    
{
 
       // Just another way which again uses str_ function..
 
       return count(str_split($string));
 
   }
}

$teststring 'One two три';

$test = new StringLib'MB' );
echo 
"\n" $test->strlen$teststring ) . "\n";
$test->testChangeLib'PHP' );
echo 
"\n" $test->strlen$teststring ) . "\n";
$test->testChangeLib'Custom' );
echo 
"\n" $test->strlen$teststring ) . "\n";

StringLib::testChangeLibStatic('MB');
echo 
"\n" StringLib::strlen$teststring ) . "\n";
StringLib::testChangeLibStatic('PHP');
echo 
"\n" StringLib::strlen$teststring ) . "\n";
StringLib::testChangeLibStatic('Custom');
echo 
"\n" StringLib::strlen$teststring ) . "\n"

(clear view at gist > https://gist.github.com/sv3tli0/76ea9e81ee73abcc271d )
At the end client will have the best way to work with strings.
Best VPS Hosting : Digital Ocean
Reply


Messages In This Thread
Suggestion: Class to handle Strings - by sv3tli0 - 09-09-2015, 12:03 AM
RE: Suggestion: Class to handle Strings - by Narf - 09-09-2015, 12:59 AM
RE: Suggestion: Class to handle Strings - by sv3tli0 - 09-10-2015, 03:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB