Did everything as in example - code doesn't work

Hi,

Project: “Going to Bed” in PHP course.

I couldn’t understand why my code doesn’t work as I thought, so I just rewrite everything from “hints”, but it anyway works wrong.

Mb you are able to clarify me, what is wrong? Thank you in advance.

Here is a code:

<?php

class StringUtilities {

  public static function secondCase($string){ 

    $string = strtolower($string);

    if (strlen($string) >= 2) {

      $string[1] = strtoupper($string[1]);

    }

    return $string;  

  }

}

class Pajamas {

  private $owner, $fit, $color;

  function __constructor($owner = "nobody", $fit = "nice", $color = "blue"){

    $this->owner = StringUtilities::secondCase($owner);

    $this->fit = $fit;

    $this->color = $color;

  }

  public function describe(){

    return "$this->owner's $this->color pajamas fit $this->fit.";

  }

}

$chicken_PJs = new Pajamas("CHICKEN", "just right", "orange");

echo $chicken_PJs->describe();

Here is an output:

's  pajamas fit .

But output should be:

cHicken's orange pajamas fit just right.

I get the exact same incorrect output and I followed the hints after a while of trying to work it out and my code was exactly the same as the posted code… Either we’re both missing something or there’s something not quite correct!

The constructor for a class in PHP is: __construct() not __constructor()
The constructor in python is: __init_().
The constructor in javascript is: constructor()

Also having an issue with this project. As far as I can tell my code is correct but it throws a fatal error for me.
An explanation of what I’m doing wrong would be great!

<?php class Pajamas{ private $owner, $fit, $color; function __construct($owner = "unclaimed", $fit = "fine", $color = "white"){ $this->owner = StringUtils::secondCase($owner); $this->fit = $fit; $this->color = $color; } public function describe(){ return "These $this->fit $this->color jammies are currently $this->owner"; } } echo Pajamas::describe(); ?>

Hey there,

You’re attempting to call your method statically, even though said method isn’t a static one.

Also, because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside methods declared as static.

My advice would be to instantiate the class (assign it to an object), and then call your method non-statically $my_obj->my_method();

1 Like

I see, I was confused by the project instructions. Must remember to do ONLY what they ask me to in the steps.
It did work when I assigned it to an object with some values, then called it.

Can I ask, is there a way to echo or access the deafult property’s values set in a contructor?

Here are some examples:

<?php class Person { public $name; public function __construct($name) { $this->name = $name; } } $person = new Person('Jack'); echo $person->name;

<?php class Person { public $name; public function __construct($name = 'John') { $this->name = $name; } } $person = new Person(); echo $person->name;

<?php class Person { public $name = 'Jim'; } $person = new Person(); echo $person->name;

That is the same problem that I am dealing with. I have been looking for the video walkthrough but could not find it.