How do you use class and object in php?

1 answer

Answer

1091947

2026-03-21 02:30

+ Follow

I believe the best way is to show you an example:

class Car{

private $gas=0;

private __construct(){

$gas=500; //when you create an object your gas tank is automatically filled with 500 gas for a start

}

public function fill_the_tank($amount){

$gas=$amoung;

}

public function get_gas(){

return $gas;

}

}

$ferrari = new Car;

echo $ferrari->get_gas(); //prints out "500"

$ferrari->get_gas(); //prints out nothing, but also doesn't end up in error

$ferrari->fill_the_tank(100);

echo $ferrari->get_gas() //prints out "600"

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.