Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Object-Oriented PHP Basics Building the Recipe Magic Methods

Diego Hernandez
Diego Hernandez
11,468 Points

Static (4 objectives)

Hello!

I'm having trouble getting though the second objective https://teamtreehouse.com/library/objectoriented-php-basics-2/building-the-recipe/static

The accepted parameter $size will be an array containing length and width in that order. Add a return value to the displayDimensions method that returns these values as a string "length x width".

class Render { public static function displayDimensions($size) {

}

}

I don't know where to place the array, I tried this from above but it didn't work: displayDimensions($size = array($length, $width)) { return ........ }

Can someone please tell me how to solve this?

Thanks1!!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The method you're defining accepts an array as a parameter. So really, you just need to access the array that's coming in and use the two elements to return the appropriate string. I'll show you how I did step 2 and then walk you through it.

<?php

class Render {

  public static function displayDimensions($size) {
    return $size[0] . " x " . $size[1];
  }   
}
?>

Here we define our method to accept a parameter named $size. The challenge explicitly says that length and width will be given in that order. Which means that length will be at the 0 index of the array, and the width will be at the 1 index of the array. But then we need to use those in conjunction with concatenation to return the elements with a " x " in between. Hope this helps! :sparkles:

Diego Hernandez
Diego Hernandez
11,468 Points

Thanks a lot Jennifer! You are the best!!!