1

I keep getting undefined variable warnings for $diameter, $surface and $circumference

<?php
class Cirkel
{
  public $diameter, $surface, $circumference; 

  public function __construct()
          {
            $this->diameter = $diameter; //$diameter is undefined
            $this->surface = $surface; //$surface is undefined
            $this->circumference = $circumference; //$circumference is undefined
          }
}
?>

I already tried passing them as parameters but than it gives me the error that too few arguments have been passed to construct?

  • 2
    Where do you expect it to get `$diameter` from? – Nigel Ren Apr 11 '21 at 07:13
  • From public $diameter, $surface, $circumference; I guess? The course I'm following didnt explain this very well so I could be missing something here. – Levi Versteeg Apr 11 '21 at 07:17
  • 1
    "_I already tried passing them as parameters_" Try that again – brombeer Apr 11 '21 at 07:17
  • You use `$this->diameter`, which refers to the public variable, without the `$this` it would expect a local varible. – Nigel Ren Apr 11 '21 at 07:18
  • As an aside why pass ( or not as the case may be ) the diameter AND the circumference? You can calculate either using the other and some basic formula. If `$surface` is the area of a circle that too can be easily calulated – Professor Abronsius Apr 11 '21 at 07:24
  • @NigelRen Can you clarify how I must pass them in that case? – Levi Versteeg Apr 11 '21 at 07:26
  • @LeviVersteeg take a look at my answer below. You could also have methods inside your class that calculate the fields from the diameter or radius and then you only have to pass one of those to the constructor. – Ivan86 Apr 11 '21 at 07:28
  • @Ivan86 Thanks for your answer. However, the assignment wants me to calculate everything using functions and apply these functions to certain objects with a given diameter. So a cd for example I would have to pass like this: $cd = new Cirkel; and then assign the diameter like this: $cd->diameter = 12; and calculate the circumference and surface area from there using functions. – Levi Versteeg Apr 11 '21 at 07:38

1 Answers1

2

You need to pass the parameters to the constructor:

<?php
    class Cirkel
    {
        public $diameter, $surface, $circumference; 

        public function __construct($diameter, $surface, $circumference)
        {
            $this->diameter = $diameter;
            $this->surface = $surface;
            $this->circumference = $circumference;
        }
    }


    $diameter = 2;
    $radius = $diameter / 2;
    $surface = $radius * $radius * pi();
    $circumference = 2 * $radius * pi();  // or $diameter * pi()

    $cirkel = new Cirkel($diameter, $surface, $circumference);
?>

Although it's best to include all the logic that is part of Cirkel as methods inside your class, something like below:

<?php
    class Cirkel
    {
        private $diameter, $radius;

        public function __construct($diameter)
        {
            $this->diameter = $diameter;
            $this->radius = $diameter / 2;
        }

        public function getCircumference() {
            return $this->diameter * pi();
        }

        public function getSurface() {
            return $this->radius * $this->radius * pi();
        }

        public function getRadius() {
            return $this->radius;
        }

        // etc.
    }

    $cirkel = new Cirkel(2);

    echo $cirkel->getCircumference();   // prints 6.2831853071796
?>
Ivan86
  • 5,695
  • 2
  • 14
  • 30
  • `3.14` is quite a low fidelity approximation of `PI` - in PHP you can call either `pi()` as a function of the constant `M_PI` for a much better approximation – Professor Abronsius Apr 11 '21 at 07:45
  • @ProfessorAbronsius true, I thought it might be sufficient as an example since it seems like a homework assignment. I'll edit, thanks. – Ivan86 Apr 11 '21 at 07:47
  • @ProfessorAbronsius It's actually much easier to read this way, and having to rewrite the constant becomes prone to error. Thanks for commenting, looks much better. – Ivan86 Apr 11 '21 at 07:54
  • This still doesn't work for me, not even when copying the exact code from your comment. It somehow still reads $diameter as undefined on line 13 and thus prints 0 as an answer. – Levi Versteeg Apr 11 '21 at 08:21
  • @LeviVersteeg I forgot to add `$this->` to the variables, I've edited to include it. Also, you don't need to explicitly add public to the functions. They are public by default, so just function should do, if you want them public. – Ivan86 Apr 11 '21 at 08:26