1
votes

I have a string manipulation class that I need in views and in controllers also! I saw that cake reuses code in Components and in Helpers for this type of situations which on my opinion breaks the OOP logic (eg. Session->read)!

Instead of doing this I created a vendor class which I imported in a StringsHelper and in a StringsComponent. I then created an identical function which instanciates the Vendor/String class and returns the results from the corresponding function. This is not quite inheritance and still redundant, but if I change code in my class it changes everywhere.

Is there a better way to do this?

2
dont use vendor clases here (since it is not a third party product), use Libs (APP/Lib) - mark
why would you care about OOP principles and concepts when you are already using CakePHP ? - tereško
I am supposed to port an entire app, developed by OOP principles! Which is now supposed to be part of a bigger app developed with cake! - amstegraf

2 Answers

2
votes

You do not need to wrap this kind of class in a Helper or a Component.

You could simply create a class with static methods and put it in APP/Lib like mentioned by Mark.

<?php
class StringTool{

  public static function manipulate($string){
   ...
  }

}

and then use it in whatever class you need, wether in a Component, a Helper, a Model, etc.

<?php
$s2 = StringTool::manipulate($s1);
0
votes

I asked this same question before. Best place is in app/Libs, where you can put a class with static helper functions that can be used anywhere in your application, including controllers and views.

Import the class using App::import('Lib', 'YourClass')

CakePHP - Where is the best place to put custom utility classes in my app structure?