Loading

It's not a dot dummy

This is one of those blindingly obvious articles and one of those short articles but it is also something I fall over constantly and am utterly blind to when coding in PHP.

It is NOT a dot, it is an arrow; ->

What am I wittering on about? My background in c# and SWIFT and pretty much every other language I code in regularly says I access properties and methods with dot notation:

object.method()
object.property

It's habit and it's automatic and its muscle memory and it's NOT PHP.

In PHP, I have a class:

class CommandHandler
{
    private $indexFile;

    /**
     * When constructed, extract the values from the request data
     */
    function __construct()
    {
        $indexFile = $this->getIndexFile();
    }

    /**
     * Returns the file name and relative path of the include file. If this is an
     * article, it will be the target article file. If not, it will be the name of
     * the index file to be rendered.
     *
     * @return string the file name and relative path
     */
    public function getTargetFile() {
        if ($this->indexFile != "")
        {
            return $this->indexFile;
        }

        return $this->targetFile;
    }

    /**
     * The request we have received may have an index file ID. If so, we need to
     * translate that in to a file name.
     *
     * @return string
     */
    private function getIndexFile() {

        if (isset($_REQUEST["index"])) {
            $indexName = $_REQUEST["index"] . '.php';
        } else {
            $indexName = "";
        }

        return $indexName;
    }

My class is called CommandHandler and it's purpose is to handle the properties from the request. It is included and instantiated using:

    <?php
        require "./codeFiles/Request/requestHandler.php";
        $requestHandler = new CommandHandler();
    ?>

This gets me an object called $resuestHandler with a bunch of properties and methods. The most important thing I want to remember from this nots is that those properties and methods are all accessed using the ARROW:

 if ($requestHandler->isIndexPage()) {
    include_once $requestHandler->getTargetFile();
    include "./codeFiles/PageStructure/footerArticle.php";

 } else if ($requestHandler->isHomePage()) {
     include "the stuff I need here"; 

 } else {
     echo "<div class=\"container\">";
    echo $requestHandler->getPageBody();
    echo "</div>";
    include "./codeFiles/PageStructure/footerArticle.php";
 }

I get whether this is an index page with:

$requestHandler->isIndexPage()

I get my target file with

$requestHandler->getTargetFile()

I actually try not to expose variables from my classes but wrap them in methods. Keeps the implementation details internal to my class. Personal preference.