开发者

accessing parent class inherited variable in subclass in PHP

开发者 https://www.devze.com 2023-01-02 01:53 出处:网络
I have made a class in php which is goind to be inherited by another class in the other folder. when i put echo $this->protectedvariableofclass; //in subclass function

I have made a class in php which is goind to be inherited by another class in the other folder. when i put echo $this->protectedvariableofclass; //in subclass function it gives no value remember my base class is stored \class\user.php sublass is stored as \model\model_user.php Please help me out Thanks in advance

Base Class in \class\user.php

<?php

class user

{
     protected $user_id;
     //setter method
public function set_user_id($user_id)
 {

  开发者_运维知识库          $this->user_id=$user_id;
 }
     //getter method
     public function get_user_id()
     {
                return $this->user_id;
     }

}
?>      

Subclass in \model\model_user.php

<?php

require_once 'class/user.php';

class model_user extends user
{
public function checkUser()

    {

   echo $this->user_id;
   $sql = "SELECT * FROM user WHERE user_id='$this->user_id'";
        $result = mysql_query($sql);
        if(!result)
        {
         die('error'.mysql_error());
        }
        $duplicates = mysql_num_rows($result);
        if($duplicates > 0)
            return 1;
        else
            return 0;

    }

}


Maybe you have done this already, but in case you're not, try this:

$model_user = new model_user();
$model_user->set_user_id(5);

$model_user->checkUser(); // Should output 5


Everything I see is that you're trying to output user_id which is not assigned anywhere in the posted code.

0

精彩评论

暂无评论...
验证码 换一张
取 消