Jump to content

missing argument - undefined variable


Pavlin24

Recommended Posts

Hallo. I have difficulties understanding an error message. It is about creating class and 2 objects. The problem is with variable $ime. I have defined it once in the class. Then I assigned other values in the objects. It shows as undefined.

Can anybody help me!

 

------------------------------------------------------------------------------------------------------------------

<?php
class Car {
public $ime="ne defenirana";
public $marka="ne defenirana";
public $model="ne definiran";
public $color="not defined";
public function __construct($ime) {
$this->ime=$ime;}
public function start($time) {
echo $ime." starts at ".$time.".";}
}
$myCar=new Car();
$myCar->ime="My car";
$myCar->marka="volkswagen";
$myCar->model="golf";
$myCar->color="grey";
$time=12.00;
$myCar->start($time);
echo "<br>";
echo "The color of my car is ".$myCar->color;
echo "<br>"."The brand of my car is ".$myCar->marka."<br>";
$myBrothersCar=new Car();
$myBrothersCar->ime="My brother's car";
$myBrothersCar->marka="volkswagen";
$myBrothersCar->model="passat";
$myBrothersCar->color="green";
$time=14.00;
$myBrothersCar->start($time);
echo "The car of my brother is ".$myBrothersCar->marka." ".$myBrothersCar->model." with color ".$myBrothersCar->color.".";
?>
---------------------------------------------------------------------------------------------------------------------------------------------------
Warning: Missing argument 1 for Car::__construct(), called in C:xampphtdocsuploadtest.php on line 13 and defined in C:xampphtdocsuploadtest.php on line 7Notice: Undefined variable: ime in C:xampphtdocsuploadtest.php on line 8Notice: Undefined variable: ime in C:xampphtdocsuploadtest.php on line 10starts at 12.The color of my car is greyThe brand of my car is volkswagenWarning: Missing argument 1 for Car::__construct(), called in C:xampphtdocsuploadtest.php on line 25 and defined in C:xampphtdocsuploadtest.php on line 7Notice: Undefined variable: ime in C:xampphtdocsuploadtest.php on line 8Notice: Undefined variable: ime in C:xampphtdocsuploadtest.php on line 10starts at 14.The car of my brother is volkswagen passat with color green.
Link to comment
Share on other sites

You have to pass a value to the constructor when you create a new instance of the class car();

 

For example:

$myCar=new Car('BMW');

 

So instead of having this line:$myCar->ime="My car";

 

Just do:$myCar=new Car('My Car');

 

When you create an instance of the class car, since the constructor is expecting an argument, you have to pass it in.

 

For the start function, looks like you forgot the keyword this-> for $ime; should be $this->ime

public function start($time){    echo $this->ime." starts at ".$time.".";}
Edited by Don E
Link to comment
Share on other sites

Thank you very much ! !

I have made corrections that you suggested. Now it is working.

 

 

 

 

 

Here is the final code:

------------------------------------------------------

<?php
class Car {
public $ime="ne defenirana";
public $marka="ne defenirana";
public $model="ne definiran";
public $color="not defined";
public function __construct($ime) {
$this->ime=$ime;}
public function start($time) {
echo $this->ime." starts at ".$time.".";}
}
$myCar=new Car("My car");
$myCar->marka="volkswagen";
$myCar->model="golf";
$myCar->color="grey";
$time=12.00;
$myCar->start($time);
echo "<br>";
echo "The color of my car is ".$myCar->color;
echo "<br>"."The brand of my car is ".$myCar->marka."<br>";
$myBrothersCar=new Car("My brother's car");
$myBrothersCar->marka="volkswagen";
$myBrothersCar->model="passat";
$myBrothersCar->color="green";
$time=14.00;
$myBrothersCar->start($time);
echo "The car of my brother is ".$myBrothersCar->marka." ".$myBrothersCar->model." with color ".$myBrothersCar->color.".";
?>
Edited by Pavlin24
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...