QUIZGUM

Coding Class

Quizgum : this in class

Using this

You should also know this before you can test the restrictor. ㅜㅜ
this is I'd like to use in my classes. Used to access a property or method declared in a class.

How to use properties using this

  1. this->propertyname;

How to use a method using this

  1. this-> method name();

Don't know what I'm talking about yet? You can use it like this:
Here is an example of using a property in a method:

  1. <?php
  2. class Car
  3. {
  4. public $wheels = 4;
  5.  
  6. public function checkWheelsCount()
  7. {
  8. return "This car has {$this->wheels} wheels.";
  9. }
  10. }
  11.  
  12. $honda = new Car;
  13. echo $honda->checkWheelsCount();
  14. ?>

Result

In other words, you should not use $wheels in a class like this: $this->wheels.
The following is an example of calling another method from a method of a class.

  1. <?php
  2. class Car
  3. {
  4. public $wheels = 4;
  5.  
  6. public function checkWheelsCount()
  7. {
  8. return $this->wheels;
  9. }
  10.  
  11. public function outputWheelsCount()
  12. {
  13. return "This car has {$this->checkWheelsCount()} wheels.";
  14. }
  15. }
  16.  
  17. $honda = new Car;
  18. echo $honda->outputWheelsCount();
  19. ?>

Result

The code above functions that the method checkWheelsCount() returns the wheels property and called checkWheelsCount() in the outputWheelsCount() method.