Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helper Class in PHP

Tags:

oop

php

I want create a helper class that containing method like cleanArray, split_char, split_word, etc.

The helper class it self will be used with many class. example :

Class A will user Helper, Class B, Class C, D, E also user Helper Class

what the best way to write and use helper class in PHP ?

what i know is basic knowledge of OOP that in every Class that use Helper class must create a helper object.

$helper = new Helper();

It that right or may be some one can give me best way to do that.

I also will create XXX Class that may use Class A, B, C, etc.

UPDATE : ->FIXED my fault in split_word method :D

Based on Saul, Aram Kocharyan and alex answer, i modified my code, but its dont work, i dont know why.

<?php
class Helper {
    static function split_word($text) {
        $array =  mb_split("\s", preg_replace( "/[^\p{L}|\p{Zs}]/u", " ", $text ));
        return $this->clean_array($array);
    }
    static function split_char($text) {
        return preg_split('/(?<!^)(?!$)/u', mb_strtolower(preg_replace( "/[^\p{L}]/u", "", $text )));
    }
}
?>

and i use in other Class

<?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->detectLanguage("ab  cd    UEEef   する ح      خهعغ فق  12  34   ٢ ٣  .,}{ + _"));
?>
like image 732
Ahmad Avatar asked Aug 15 '11 12:08

Ahmad


People also ask

What is a helper class in PHP?

Helper classes are usually a sign of lack of knowledge about the Model's problem domain and considered an AntiPattern (or at least a Code Smell) by many. Move methods where they belong, e.g. on the objects on which properties they operate on, instead of collecting remotely related functions in static classes.

What is helper class used for?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).

What is helper class in Laravel?

A helper function usually comes in handy if you want to add a feature to your Laravel application that can be used at multiple locations within your controllers, views, or model files. These functions can be considered as global functions. Laravel includes with many helper functions by default.

What is difference between helper and utility class?

A Utility class is understood to only have static methods and be stateless. You would not create an instance of such a class. A Helper can be a utility class or it can be stateful or require an instance be created. Create Utility class if you want to have methods that are used by several operations.


1 Answers

Helper classes are usually a sign of lack of knowledge about the Model's problem domain and considered an AntiPattern (or at least a Code Smell) by many. Move methods where they belong, e.g. on the objects on which properties they operate on, instead of collecting remotely related functions in static classes. Use Inheritance for classes that share the same behavior. Use Composition when objects are behaviorally different but need to share some functionality. Or use Traits.

The static Utils class you will often find in PHP is a code smell. People will throw more or less random functions into a class for organizing them. This is fine when you want to do procedural coding with PHP<5.2. As of 5.3 you would group those into a namespace instead. When you want to do OOP, you want to avoid static methods. You want your objects to have High Cohesion and Low Coupling. Static methods achieve the exact opposite. This will also make your code less testable.

  • Are Helper Classes Evil?
  • Killing the Helper class, part two
  • Functional Decomposition AntiPattern
  • Is the word "Helper" in a class name a code smell?

Moreover, every Class that use Helper class must create a helper object is a code smell. Your collaborators should not create other collaborators. Move creation of complex object graphs into Factories or Builders instead.

like image 71
Gordon Avatar answered Sep 21 '22 15:09

Gordon