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 Static

Accessing 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());
index.php
<?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
Spenser Hale
20,778 Points

Hello 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
jamesjones21
9,248 Points

As this is a challenge I don't think type hinting would be allowed unfortunately

Spenser Hale
Spenser Hale
20,778 Points

Hey 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:

<?php
class Render {
    public static function displayDimensions($size)
    {
        return $size[0].' x '.$size[1];
    }
    public static function detailsKitchen($room)
    {
        return 'Kitchen Dimensions: ' . self::displayDimensions($room->getDimensions());
    }
}
jamesjones21
jamesjones21
9,248 Points

The 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 :)

Thanks 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
jamesjones21
9,248 Points

it 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.

Thanks, 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
Spenser Hale
20,778 Points

Hey 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();
}

Hey, 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
Spenser Hale
20,778 Points

Hey 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!