Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

design patterns - Why don't PHP developers cache their methods?

I'm hoping this question isn't too opinionated for StackOverflow, it seems like there's a correct answer here that I just can't see.

I've seen a few other languages do this, but PHP is the one I'm looking at now. This question stems from me looking at the Magento 2 source code:

<?php
class Image extends AbstractHelper implements ArgumentInterface
{

    // ...

    protected function setImageProperties()
    {
        $this->_getModel()->setDestinationSubdir($this->getType());
        $this->_getModel()->setWidth($this->getWidth());
        $this->_getModel()->setHeight($this->getHeight());

        // Set 'keep frame' flag
        $frame = $this->getFrame();
        $this->_getModel()->setKeepFrame($frame);

        // Set 'constrain only' flag
        $constrain = $this->getAttribute('constrain');
        if (null !== $constrain) {
            $this->_getModel()->setConstrainOnly($constrain);
        }

        // Set 'keep aspect ratio' flag
        $aspectRatio = $this->getAttribute('aspect_ratio');
        if (null !== $aspectRatio) {
            $this->_getModel()->setKeepAspectRatio($aspectRatio);
        }

        // Set 'transparency' flag
        $transparency = $this->getAttribute('transparency');
        if (null !== $transparency) {
            $this->_getModel()->setKeepTransparency($transparency);
        }

        // Set background color
        $background = $this->getAttribute('background');
        if (null !== $background) {
            $this->_getModel()->setBackgroundColor($background);
        }

        return $this;
    }

(Taken from github)

Again and again, you see $this->_getModel(). I'd have thought it would be more efficient to store the results of that method in a variable and then call it as needed:

<?php
class Image extends AbstractHelper implements ArgumentInterface
{

    // ...

    protected function setImageProperties()
    {
        $model = $this->_getModel();
    
        $model->setDestinationSubdir($this->getType());
        $model->setWidth($this->getWidth());
        $model->setHeight($this->getHeight());

        // ...
    }

And yet, I rarely see server-side programmers do this. Is there a reason why they don't? Is it considered more readable? Does the compiler abstract away the overhead so the two options are as efficient as each other (or even more efficient since there's no variable assignment)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ran some tests on the looping to conclude when to performance

class Test{
    private function someFunc()
    {
        $myString = "any string";
        return $myString;
    }
    public function doTestCall()
    {
        for ($i = 0; $i < 150000; $i++) {
            $x = $this->someFunc();
        }
    }
    
    public function doTestWithoutCall()
    {
        $y = $this->someFunc();
        for ($i = 0; $i < 150000; $i++) {
            //nothing
        }
    }
}

doTestCall: 0.25357
doTestWithoutCall: 0.019944

and again:

doTestCall: 0.250552
doTestWithoutCall: 0.019927

and again:

doTestCall: 0.24839
doTestWithoutCall: 0.020015

When the loop ran a million times:

doTestCall: 0.45552
doTestWithoutCall: 0.134747


doTestCall: 0.47562
doTestWithoutCall: 0.129841

From this it can be concluded that data retention is faster? However at rather negligible periods of time - if it is a function that does not perform a complex operation. So I allow myself to assume that the preference is for a more readable code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...