Pergunta

I'm little confused with OOPS in php. i'm trying to understand set get methods in php. here simple zend framework module that on i'm looking

https://github.com/blanchonvincent/SimplePageCrawler

public function getHeadingTags()
{
    if(null === $this->headingTags) {
        $this->setHeadingTags(new ArrayObject()); // here called but empty array passed
    }
    return $this->headingTags;
}

public function setHeadingTags($headingTags)
{
    if(is_array($headingTags)) {
        $this->getHeadingTags()->exchangeArray($headingTags);
        return $this;
    }
    if(!$headingTags instanceof ArrayObject) {
        throw new Exception\InvalidArgumentException('Heading tags must be an array or an ArrayObject');
    }
    $this->headingTags = $headingTags;
    return $this;
}
$crawler = $this->getServiceLocator()->get('SimplePageCrawler');
$page = $crawler->get('http://cnn.com');
print_r($page->getHeadingTags()); // this method return results very well

but I'm trying to understand, how it returns results without calling setHeadingTags method. may be its called auto.

please help me to understand. how it called thank you

Foi útil?

Solução

setHeadingTags is purely a setter on the response class. In the getter, it places an ArrayObject in it if it is empty. This gives you, the developer, the option to place items in the header using the setter if you wish, if not, then it will be populated for you when getHeadingTags is called.

The answer to your question: It isn't called automatically, or ever, other than when it attempts to get the tags, at which point it is checked for being null, if so populate with empty object, then return empty object. If for example you had wanted specific header tags added, you would call it from your code with the values you wanted, at which point the getter would notice it wasn't null and return your header tags.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top