Note that there are some explanatory texts on larger screens.

plurals
  1. POHelper Class in PHP
    text
    copied!<p>I want create a helper class that containing method like cleanArray, split_char, split_word, etc.</p> <p>The helper class it self will be used with many class. example :</p> <pre><code>Class A will user Helper, Class B, Class C, D, E also user Helper Class </code></pre> <p>what the best way to write and use helper class in PHP ?</p> <p>what i know is basic knowledge of OOP that in every Class that use Helper class must create a helper object.</p> <pre><code>$helper = new Helper(); </code></pre> <p>It that right or may be some one can give me best way to do that.</p> <p>I also will create XXX Class that may use Class A, B, C, etc.</p> <p><strong>UPDATE :</strong> ->FIXED my fault in split_word method :D</p> <p>Based on Saul, Aram Kocharyan and alex answer, i modified my code, but its dont work, i dont know why.</p> <pre><code>&lt;?php class Helper { static function split_word($text) { $array = mb_split("\s", preg_replace( "/[^\p{L}|\p{Zs}]/u", " ", $text )); return $this-&gt;clean_array($array); } static function split_char($text) { return preg_split('/(?&lt;!^)(?!$)/u', mb_strtolower(preg_replace( "/[^\p{L}]/u", "", $text ))); } } ?&gt; </code></pre> <p>and i use in other Class</p> <pre><code>&lt;?php include "Helper.php"; class LanguageDetection { public function detectLanguage($text) { $arrayOfChar = Helper::split_char($text); $words = Helper::split_word($text); return $arrayOfChar; } } $i = new Detection(); print_r($i-&gt;detectLanguage("ab cd UEEef する ح خهعغ فق 12 34 ٢ ٣ .,}{ + _")); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload