John Holmes
John Holmes

Reputation: 13

When I run the code below error comes

The code has employee details to be assigned and a function prints all the details when called using an instance. When I call the method from the object that I created error comes I want you guys to help me.

  <?php
     /*
      * To create a class which adds employee details
      * value has to be printed when function is called
      */
     class Emp{
         /* All variables */
         var $Empname;
         var $Emp_id;
         var $DOB;
         var $Dept;
         var $floor;

         public function get_details($Empname,$Emp_id,$DOB,$Dept,$floor)
         {
              //Error comes here value not gets printed//
              /* Function printing all the details */
              echo 'Your name is '.$this->$Empname."<br/>";
              echo 'Your id is'.$this->$Emp_id."<br/>";
              echo 'Your DOB is'.$this->$DOB."<br/>";
              echo 'your Dept is'.$this->$Dept."<br/>";
              echo 'your floor is'.$this->$floor."<br/>";         
          }
      }

      $obj=new Emp();
      //Is there any wrong in the below function call//
      $obj->get_details('John',1041541,'Feb-12','Customs',4);             
   ?>

Upvotes: 1

Views: 45

Answers (3)

xdazz
xdazz

Reputation: 160843

Change

echo 'Your name is '.$this->$Empname."<br/>";

to

echo 'Your name is '.$Empname."<br/>";

The property in your class is never assigned, so using $this->Empname will be null. ($this->$Empname is even wrong, you are trying to get the property whose name is John)

But, what you should do indeed is to provide a constructor method for Emp:

class Emp{
  /* All variables
   */
  var $Empname;
  var $Emp_id;
  var $DOB;
  var $Dept;
  var $floor;

  public function __construct($Empname,$Emp_id,$DOB,$Dept,$floor) {
    $this->Empname = $Empname;
    // and so on...
  }

  public function get_details()
  {
    //Error comes here value not gets printed//
    /* Function printing all the details
     */
    echo 'Your name is '.$this->Empname."<br/>";
    echo 'Your id is'.$this->Emp_id."<br/>";
    echo 'Your DOB is'.$this->DOB."<br/>";
    echo 'your Dept is'.$this->Dept."<br/>";
    echo 'your floor is'.$this->floor."<br/>";
  }
}

// then use it like below
$obj=new Emp('John',1041541,'Feb-12','Customs',4);
$obj->get_details();

Upvotes: 1

The $ should be removed and you need to do the assignment. !

    echo 'Your name is '.$this->$Empname."<br/>";

Something like this...

   echo 'Your name is '.$this->Empname = $Empname ."<br/>";           ^

Do this for all of your echo statements.

The fixed code...

<?php

class Emp{

    var $Empname;
    var $Emp_id;
    var $DOB;
    var $Dept;
    var $floor;
    public function get_details($Empname,$Emp_id,$DOB,$Dept,$floor)
    {
        echo 'Your name is '.$this->Empname = $Empname ."<br/>";
        echo 'Your id is'.$this->Emp_id=$Emp_id."<br/>";
        echo 'Your DOB is'.$this->DOB=$DOB."<br/>";
        echo 'your Dept is'.$this->Dept=$Dept."<br/>";
        echo 'your floor is'.$this->floor=$floor."<br/>";

    }

}

$obj=new Emp();
$obj->get_details('John',1041541,'Feb-12','Customs',4);

OUTPUT :

Your name is John
Your id is1041541
Your DOB isFeb-12
your Dept isCustoms
your floor is4

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

echo 'Your name is '.$this->$Empname."<br/>";
echo 'Your id is'.$this->$Emp_id."<br/>";
echo 'Your DOB is'.$this->$DOB."<br/>";
echo 'your Dept is'.$this->$Dept."<br/>";
echo 'your floor is'.$this->$floor."<br/>";

Is wrong and should be

echo 'Your name is '.$this->Empname."<br/>";
echo 'Your id is'.$this->Emp_id."<br/>";
echo 'Your DOB is'.$this->DOB."<br/>";
echo 'your Dept is'.$this->Dept."<br/>";
echo 'your floor is'.$this->floor."<br/>";

In addition you are using var in the class, get rid of it since they are no longer used and try using public, protected ...etc.

http://www.php.net/manual/en/language.oop5.visibility.php

Upvotes: 1

Related Questions