当我运行下面的代码时出现错误

时间:2014-03-19 07:31:48

标签: php

代码包含要分配的员工详细信息,并且函数在使用实例调用时打印所有详细信息。 当我从我创建的对象调用该方法时出现错误,我希望你们帮助我。

  <?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);             
   ?>

3 个答案:

答案 0 :(得分:1)

应删除$,您需要进行分配。 !

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

像这样......

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

对所有echo语句执行此操作。

固定代码......

<?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

答案 1 :(得分:1)

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/>";

此外,你在课堂上使用var,因为它们不再使用而摆脱它,并尝试使用public,protected ...等。

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

答案 2 :(得分:1)

更改

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

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

您的课程中的媒体资源永远不会分配,因此使用$this->Empnamenull。 ($this->$Empname甚至是错误的,您正试图获取名称为John的商品

但是,你应该做的确是为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();