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 trialnathanielamponsahmanu
342 PointsAccessing a static method from another static method in the same class
Hi,
I'm trying to access method displayDimensions() from detailsKitchen() but the workspace gives me an error.
I have run the code on my desktop and I think it works. Any ideas?
Bummer: You use the keyword "self" instead of "this" when accessing a static method.
TIA
class Render {
private $dimensions = array();
public function addDimension($string)
{
$this->dimensions[] = $string;
}
public function getDimensions()
{
return $this->dimensions;
}
public static function displayDimensions($size)
{
$output = '';
$output .= $size[0];
$output .= ' x ';
$output .= $size[1];
return $output;
}
public static function detailsKitchen($room)
{
$output = 'Kitchen Dimensions: ';
$output .= self::displayDimensions($room);
return $output;
}
}
$room = new Render();
$room->addDimension("12");
$room->addDimension("14");
echo Render::detailsKitchen($room->getDimensions());
<?php
class Render {
private $dimensions = array();
public function addDimension($string)
{
$this->dimensions[] = $string;
}
public function getDimensions()
{
return $this->dimensions;
}
public static function displayDimensions($size)
{
$output = '';
$output .= $size[0];
$output .= ' x ';
$output .= $size[1];
return $output;
}
public static function detailsKitchen($room)
{
$output = 'Kitchen Dimensions: ';
$output .= self::displayDimensions($room);
return $output;
}
}
$room = new Render();
$room->addDimension("12");
$room->addDimension("14");
echo Render::detailsKitchen($room->getDimensions());
?>
5 Answers
Spenser Hale
20,915 PointsHello Nathaniel.
Unfortunately you are overthinking what the challenge is asking of you. You do not have to define getDimensions nor addDimension, their test will passing an object into detailsKitchen for you, specifically the Room object from another section of their course.
<?php
class Render {
public static function displayDimensions(array $size): string
{
return $size[0].' x '.$size[1];
}
public static function detailsKitchen(Room $room): string
{
return 'Kitchen Dimensions: ' . self::displayDimensions($room->getDimensions());
}
}
jamesjones21
9,260 PointsThe thing with code challenges they stump a lot of people, but we can use vsr_dump to read what is currently in the $size array, but it's hard to figure out if you can't see them in order to use them :) but the above example is how I would code it too, using type hinting is great an makes code a lot easier to read an that we are setting those parameters to what is declared :)
nathanielamponsahmanu
342 PointsThanks Guys.
@Spenser final code helped and I'm now proceeding with the course.
I'm a bit confused about this sytax = public static function displayDimensions(array $size): string
what does this mean: displayDimensions(array $size): string
Thanks to all of you for the time
jamesjones21
9,260 Pointsit basically means that the parameter $size is required to be an array, so when the function displayDimensions() is called, the argument that goes inside it will need to be an array, anything else such as a string, integer or boolean will through a type mismatch error.
Hope this helps.
nathanielamponsahmanu
342 PointsThanks, James,
That helps.
So basically I can set what type of parameter to accepted for $size displayDimensions(array $size): string
But what about this - funstionName(): string
Spenser Hale
20,915 PointsHey Nathaniel,
I believe you are inquiring about the return type.
functionName(): string states that the functionName will always return a string.
The syntax is colon and then a type.
Examples:
<?php
function returnString(): string {
return 'I am a string';
}
function returnInt(): int {
return 2;
}
function returnArray(): array {
return ['item'];
}
function returnClass(): ExampleClass {
return new ExampleClass();
}
nathanielamponsahmanu
342 PointsHey, you learn something every day.
Thanks guys, I did know this --- but now I do!
So the return type does not change the value, it just checks and throws an error if not the correct required value?
Spenser Hale
20,915 PointsHey Nathaniel
Correct, the PHP interpreter will check to see if value matches and throw an Error if it does not match, specifically a TypeError.
Glad we were able to help you learn some more PHP!
jamesjones21
9,260 Pointsjamesjones21
9,260 PointsAs this is a challenge I don't think type hinting would be allowed unfortunately
Spenser Hale
20,915 PointsSpenser Hale
20,915 PointsHey James Jones
Thank you for looking out for students, however I ran this code through the challenge to ensure it worked before providing the answer. I added type hinting because I thought it might better show Nathaniel and other students that for the detailsKitchen method, we are accepting the Room object.
I doubt in the future TeamTreeHouse will drop PHP support to be below 7.1, but I agree it might be also useful for other students to have code without type hinting: