Why does the titles aren't printed?

I’m very new to PHP. I tried to write this OOP code of a Recipe class and some recipe instances but the recipe titles aren’t appearing and I’d like to ask a PHP programmer why does the getTitle() getter function fails to print the title given in it’s call as an argument.

It’s surly not a lack of an parameter (I know I shouldn’t give a parameter to a getter).

class Recipe {
    private $title;
    public $author = "Me myself";
    public $ingredients = array();

    private function setTitle($title) {
        echo $this->title = ucwords($title) . '<br>';
    }
    public function getTitle() {
        $this->title;
    }
}

$recipe1 = new Recipe();
    $recipe1->getTitle("the monk who sold his ferrari");
    echo '<br>';
    echo $recipe1->author;
    echo '<br>';

$recipe2 = new Recipe();
    $recipe2->getTitle("the authentic tunisian cuisine");
    echo '<br>';
    echo $recipe2->author = "A.B";
    echo '<br>';

Hi @Benos, your method getTitle does not do anything and I cannot see you’re calling setTitle anywhere. I Think your’e looking for something like this:

class Recipe 
{
	private $title;
	public $author = "Me myself";
	public $ingredients = array();

	public function getTitle() 
	{
		return $this->title;
	}

	public function setTitle($title) 
	{
		$this->title = ucwords($title) . '<br>';
	}

	public function printTitle() 
	{
		echo $this->title;
	}
}

$recipe1 = new Recipe();
$recipe1->setTitle("the monk who sold his ferrari");
$recipe1->printTitle();

Hi Andreas. Thank you. Must one use printTitle()? Can’t the printing be done from getTitle() directly or shouldn’t it be done from it? I’m not sure which question is better than these two in a “traditional” OOP perspective.

I mean, I’m not sure if from that “traditional” perspective we need a setter, getter, and printer, or could do well just with a setter and getter.

Well no, it’s really up to you but I was trying to follow and illustrate this programming principle:
Functions or methods should just do one thing… preferably just what it says on the tin (function name). If you start doing lots of other unexpected things then it will be much harder to follow what your code does. By writing more functions that do less instead of the opposite you have much higher chance of being able to reuse your code. It will also be much easier to understand what is happening. A setter does not conventionally print anything, it just sets a value of a property of an object.

Also…

echo $this->title = ucwords($title) . '<br>';

I don’t think the code above will print the title as you’re echoing not the property but the assignment of a value to the property. That may just echo ‘1’ for true as in the operation was successful but I do not really know exactly.

In my opinion (with little real-world PHP experience) it should not. You may want to echo it differently in different ways in future code (what if you don’t always want a <br> after it, for example), or you might want to just use it in a calculation, which would mean having a different function for each way. Far better (again IMO) to have something return the value and then deal with it locally.

Saying that, I probably wouldn’t have had the printTitle() function at all, just used

echo $recipe1->getTitle() . "<br>";

And I probably wouldn’t store it with the appended <br> in the setTitle() function, again in case it’s not always required.

I always thought that too, you’re just echoing the result of assigning that string. But it seems to not work that way. For example:

<?php
echo "Hello";
echo $x = "test";
echo $x;
?>

results in

Hellotesttest

The documentation clarifies it:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.