1
\$\begingroup\$

I have this constructor (from a Service) where I'm setting a few variables, but two of them include HTML. Is there a best way to set those values, maybe by creating a method in the class to set them?

public function __construct($builder, $id, $nickname, $title, $options)
{
        $this->builder     = $builder;
        $this->id          = $id;
        $this->icon        = '<i class="fa fa-circle-o"></i>'; // This one
        $this->title       = '<span>'.$title.'</span>'; // This one as well
        $this->nickname    = camel_case(Str::ascii($nickname));
        $this->attributes  = $this->builder->extractAttributes($options); 
}
\$\endgroup\$
2
  • \$\begingroup\$ As it is not depends on the parameter send by the user and it is default icon then you can directly initialized as default value to this property in class only instead of initializing in constructor. As before __construct you can define as $this->icon = '<i class="fa fa-circle-o"></i>' \$\endgroup\$ – Alankar More Feb 5 '16 at 10:44
  • \$\begingroup\$ '<span>'.$title.'</span>' you can define function which will return the $title wrapped in tags you want. if in future if you want to apply some more modifications on this $title as per the design perspective you just have to make changes in your function. \$\endgroup\$ – Alankar More Feb 16 '16 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.