Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

matt c
matt c
267 Points

PHP OOP 3/5 challenge

In challenge 3/5 in the php OOP basics course, after declaring the constructor and assigning the parameters to the class properties, why is the answer acceptable like this:

Class Fish {

public $name;

public function __construct() { $this->name = $name; //this order is seemingly okay }

public function __construct() { $name=$this->name; /but in this order the answer is not
accepted
/ }

Is the second way still acceptable OOP PHP syntax?

3 Answers

matt c
matt c
267 Points

Yep, single line comments are forward slashes. backslashes are used for escaping characters etc.

The point was not the comments - but the validity of the order $this->name = $name; vs. $name = $this->name; which are the same, however, in the quiz, one throws an error and the other is accepted.

Ah, they're not quite the same.

$this->name = $name will assign $this->name the value of $name.

E.g. if $name is an empty string and $this->name is Matt, after running this line of code, $this->name will be set to an empty string. The inverse is also true.

The equal operator will assign the value of the right hand side to the left hand side which is why it's only valid in a particular sequence.

Have I made much sense there?

I think the only issue is with your comments.

Single Line Comments:

\\ Comment here
$not_comment

Multi-line comments:

\* This is a 
multi-line comment.
                   ****
         It ends like this. */

Your comments either needs to start with two slashes or be contained as above.

Single line comments are // !

<?php

// Single line comment

/*
Multi line comment
 */

/**
 * 'Doc Block' will initiate special syntax highlighting in some code software. 
 * Some external software can parse these comments and create automatic
 *  documentation. It starts with **
 *
 * @return string
 */
matt c
matt c
267 Points

Yes, you have!

This is a very important point to know.

Thank you!

No worries!