0

so i was working and i intuitively typed this just before the break

  use Carousel\Carousel;

class FyStories

{
    protected Carousel $carousel;

    public function __construct()
    {
        $this->carousel = new Carousel();
    }
}

i came back and i didn't understand what's the difference between protected Carousel $carousel; and this

    public function __construct()
    {
        $this->carousel = new Carousel();
    }

i know a construct is where you set everything you want to be initialized within the class; but are the first piece of code and the second the same thing are they different how do the first protected Carousel $carousel do ??

  • Does this answer your question? [What is the difference between public, private, and protected?](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) – Markus Zeller Jun 26 '22 at 08:21
  • @MarkusZeller actually no, i meant what typecasting a variable with a class actually do , does it do the same thing as the construct in the background (like some magic ) –  Jun 26 '22 at 11:01
  • `protected Carousel $carousel;` just says, the carousel property ONLY can have a value with instance of Carousel. If you put something different, the program will crash. `$this->carousel = new Carousel();` is ok, because you pass in the correct instance. This is all the same for private or public scope. – Markus Zeller Jun 26 '22 at 12:48
  • For further reference, these are called [type declarations](https://www.php.net/manual/en/language.oop5.properties.php) although you’ll often see people refer to them as “type hints” which is from when PHP couldn’t actually enforce them, but is now semantically incorrect. – Chris Haas Jun 26 '22 at 13:30

0 Answers0