QUIZGUM

Coding Class

Quizgum : creating instance

creating instance

To use a declared class, create an instance.
Creating an instance creates an object variable of the class.
You can think of the class declared earlier as a rubber stamp.
In other words, a single stamp on a red pad is stamped in red, and a green pad is stamped in green
. That means you can make a Porsche.
Instances are declared outside the class and use the new keyword.

How to create an instance

$variablename = new classname;   

Then, following the previous course ...
Let's create an instance of the car class declared earlier.

<?php
    class Car
    {
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        public function run()
        {
            return "The car is running.";
        }

        protected function stop()
        {
            return "car stops.";
        }

        protected function turn()
        {
            return "turnning car";
        }
    }
    $honda = new Car;
?>

So let's check with var_dump() what object the honda instance has in the code above.

<?php
    class Car
    {
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        public function run()
        {
            return "The car is running.";
        }

        protected function stop()
        {
            return "car stops.";
        }

        protected function turn()
        {
            return "turnning car";
        }
    }

    $honda = new Car;

    var_dump($honda);
?>

When I run it, I get the following output:
object(Car)#1(5) { ["wheels"]=> NULL ["doors"]=> int(4) ["color":protected]=> int(4) ["size":"Car":private]=> NULL ["company":"Car":private]=> NULL }

an we access properties and methods through instances? Use-> to access. The honda variable is declared outside of the class, so it has access to properties and methods declared public.

Using properties

$variable = new classname;
$variable->property;

Note that we use the $in front of the property.

<?php
    class Car
    {
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        public function run()
        {
            return "The car is running.";
        }

        protected function stop()
        {
            return "car stops.";
        }

        protected function turn()
        {
            return "turnning car";
        }
    }

    $honda = new Car;
    echo $honda->doors;

?>

You can see the value of the doors property is 4.

Now shall we call the method?

Using the method

$variable name = new class name;
$variable name->method name();

Since a method is a function, make sure to add() after the method name.
If not attached, it will be recognized as a property.

<?php
    class Car
    {
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        public function run()
        {
            return "The car is running.";
        }

        protected function stop()
        {
            return "car stops.";
        }

        protected function turn()
        {
            return "turnning car";
        }
    }

    $honda = new Car;
    echo $honda->run();

?>

Result of the code above

Depending on the access modifier, learning more is more important to know where it is available or not. Let's test it. To do that, we need to know inheritance first. You need to know about inheritance before you can use protected. Next, let's look at inheritance and then test access constraints.