Basic OOP Question

I’m confused why you would call a method without sending it any parameters, particularly when this method has no default parameters of its own.


private function replaceDBTags($tag,$cacheId) {
     // Call to getAdditionalParsingData with empty params
     $apd = $this->page->getAdditionalParsingData();

//Method inside Page object:
public function getAdditionalParsingData() {

       return $this->apd;

It seems to me these two blocks of code accomplish nothing together.

Think of properties as pieces that make a whole object. In an object oriented fashion, it’s common to create objects that are composed of other objects, or use other objects to get an end result. If a property does not immediately relate to that object, it’s better to put it in another class that the object would use.

Also, you should understand the point of encapsulation. if that apd variable in the page class is private, the only way you could retrieve it is through its own public get function that retrieves it for you. In that sense, that apd variable becomes immutable (it can’t change if you can’t access it). It’s a very common practice.

Thank you very much for the prompt reply. I do have a very vague understanding of the concept of composition. I’m still confused though what data is being passed and manipulated in an instance like this though.

Please forgive me if your reply has already answered this question.

In this example is the private function replaceDBTags() calling getAdditionalParsingData() only to store an instance of itself?

Is that all the replaceDBTags function does? If so then yes it’s odd. I’m not sure what else it does, but my guess is that it’s supposed to do something with the parsed data and return some result.

The whole purpose of the replaceDBTags method is to find comment tags in an html template. Looking ahead in the book I’m studying from, a comment would look something like:
<!–START comments-{status_id}–>

It appears the method would then replace this tag with actual data cached from the DB.

There’s a good bit more this replaceDBTags() does as far as conditionals, as a template may contain nested comments inside one and other. But replacing these tags with real data is the main focus of the method.

Thanks again for your insights.

the replaceDBTags function can only be accessed by the script however, the
getAdditionalParsingData function can be accessed by the user, so you’re just invoking the private function indirectly - which probably has its own default variables or getting it from somewhere else, so variables are being passed, just not by the user.

So as I understand it we are storing in getAdditionalParsingData() a copy of replaceDBTags() under the variable name $apd for the user to interact with.

If this is correct, I do have 2 further questions.

  1. Is this a form of caching? It seems every time replaceDBTags() is run $apd will be overwritten with a new value.

  2. getAdditionalParsingData() is called very early in my private method. There is still a lot of functionality left to execute. Will the entire replaceDBTags() method be saved in $apd, or just the parts run up until the point getAdditionalParsingData() is called and the $apd variable is created?