HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Calling a method outside class statically, passing other method's output as parameter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodpassingotheroutputstaticallyoutsidecallingclassparameter

Problem

I have this class:

class adminTemplate {

    public static function load() 
    {
        return new self();
    }

    public function sidebarGuide()
    {
        return 'content'; // Very long html markup
    }

    public function wrapSidebars($content)
    {
        ob_start();
        echo "";
        echo $content;
        echo "";
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }

    // Other methods here

}


Somewhere in my script, I have to call wrapSidebars using sidebarGuide's output as parameter:

echo adminTemplate::load()->wrapSidebars(adminTemplate::load()->sidebarGuide());


It works, but I'm pretty confident there is a more appropriate way.

  • Is there a proper way of passing methods'output as other method's parameter in a class loaded "statically"?



  • Besides that, is there a more elegant code structure to accomplish the same "task"?

Solution

You are calling load() every time you call either sidebarGuide or wrapSidebars. Are you doing anything other than returning an instance of the class? If not, then just instantiate the class and making load() a construct would be more ideal. Doing so, your class would look like:

class adminTemplate {
    public function __construct(){}

    public function sidebarGuide(){
        return "content"; # Very long html markup
    }

    public function wrapSidebars($content){
        ob_start();
        echo "";
        echo $content;
        echo "";
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
}


You would then use it as follows:

$adminTemplate = new adminTemplate();
$adminTemplate->sidebarGuide();
$adminTemplate->wrapSidebars($adminTemplate->sidebarGuide());


If you wanted, you could even store the return value of sidebarGuide() in a property of the class so that you could call wrapSidebars() without injecting sidebarGuide(), as follows:

class adminTemplate {
    private $sidebarGuideReturnValue

    public function __construct(){}

    public function sidebarGuide(){
        $this->sidebarGuideReturnValue = "content"; # Very long html markup

        return $this->sidebarGuideReturnValue;
    }

    public function wrapSidebars($content = ""){
        # Make $content an optional parameter. If its not set, grab the property
        $content = ($content ? $content : $this->sidebarGuideReturnValue);

        ob_start();
        echo "";
        echo $content;
        echo "";
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
}


If you know that would only be calling sidebarGuide() exclusively as a param to wrapSidebars(), you could even relegate sidebarGuide()'s to a private called on instantiation in the construct, or move its body to the construct itself.

One question that bothers me though is, is there any specific reason you want to use static load()? Or just because it looks nice?

Code Snippets

class adminTemplate {
    public function __construct(){}

    public function sidebarGuide(){
        return "content"; # Very long html markup
    }

    public function wrapSidebars($content){
        ob_start();
        echo "<div class='sidebar wrapper'>";
        echo $content;
        echo "</div>";
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
}
$adminTemplate = new adminTemplate();
$adminTemplate->sidebarGuide();
$adminTemplate->wrapSidebars($adminTemplate->sidebarGuide());
class adminTemplate {
    private $sidebarGuideReturnValue

    public function __construct(){}

    public function sidebarGuide(){
        $this->sidebarGuideReturnValue = "content"; # Very long html markup

        return $this->sidebarGuideReturnValue;
    }

    public function wrapSidebars($content = ""){
        # Make $content an optional parameter. If its not set, grab the property
        $content = ($content ? $content : $this->sidebarGuideReturnValue);

        ob_start();
        echo "<div class='sidebar wrapper'>";
        echo $content;
        echo "</div>";
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
}

Context

StackExchange Code Review Q#60490, answer score: 2

Revisions (0)

No revisions yet.