0

let's say I have the following php classes: class user, class fileManager and class downloader extends fileManager

<?php
class user {
  private $name;
  public function __construct($name){
     $this->_name = $name;
  }
  public function getName(){
     return $this->_name;
  }
}
class fileManager {
  protected $user, $list;
  public function __construct($user, $list){
    $this->_user = $user;
    /* IF list is 1 OR 2... */
    $this->_list = $list
    /* for test: */
    echo $this->user->getName(); /* WORKS */
  }
  /* SOME METHODS */
}
class downloader extends fileManager {

  public function download($fileid){
    $this->_fileid = $fileid;
    /* if his username is set, he is logged in */
    if($this->user->getName()!=null){continue();}
  }
}

$user = new user('Jan');
echo $user->getName(); /* = jan */
$fm = new fileManager($user, 1);
$down = new downloader();
$down->download(1); 
/* FATAL ERROR, Call to a member function getName() on a non-object */
?>

So, jan is logged in, and opens the filemanager. The filemanager calls getName from the user class and knows thats it's jan. But if Jan wants to download file 1, the downloader doesn't recognize Jan. Why ? Aren't child classes supposed to inherit properties from parent classes?

2
  • You set $_user but address $user. Commented Jan 3, 2014 at 11:27
  • 1
    You don't seem to understand inheritance: downloader extends the class filemanager, not a specific instance of the filemanager class Commented Jan 3, 2014 at 11:29

2 Answers 2

1

$down is a new object. -> The constructor needs params. Try

$user = new user('Jan');
echo $user->getName(); /* = jan */
$fm = new fileManager($user, 1);
$down = new downloader($user, 1);
$down->download(1);
Sign up to request clarification or add additional context in comments.

8 Comments

You don't particularly even need $fm = new fileManager($user, 1);
i thought he may use it elsewhere
Thank you, I know it will work, but why can't the downloader class access the $user property from the fileManager class?
come on - lern about inheritance first
Yes, I need fileManager class, the code in the question is shrinked.
|
0

Not considering the typos, you have to pass values to the constructor when you instantiate the download class.

class user {
  private $name;
  public function __construct($name){
     $this->_name = $name;
  }
  public function getName(){
     return $this->_name;
  }
}
class fileManager {
  protected $user, $list;
  public function __construct($user, $list){
    $this->_user = $user;
    /* IF list is 1 OR 2... */
    $this->_list = $list;
    /* for test: */
    echo $this->_user->getName(); /* WORKS */
  }
  /* SOME METHODS */
}
class downloader extends fileManager {


  public function download($fileid){
    $this->_fileid = $fileid;
    /* if his username is set, he is logged in */
    if($this->_user->getName()==null)
        return false;

    //download file

  }
}

$user = new user('Jan');
echo $user->getName(); /* = jan */
$fm = new fileManager($user, 1);
$down = new downloader($user, 1);
$down->download(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.