0

I want to have an object for a student that contains their name among other properties – one of these properties being their assignment. And for the assignment I want to create another object containing its details.

class Student {
    private $id;   // used for sql queries
    private $name; // result from such query

    public function __construct($id) {
        $this->id = $id;
        // get properties from db using the $id
    }

    public function getName() {
        return $this->name;
    }
}

class Assignment {
    private $id; // used for sql queries
    private $student; // result from query
    private $details; // result from query

    public function __construct($id) {
        $this->id = $id;
        // get properties from db using the $id
        $this->student = new Student($row["studentId"]);
    }

    public function getDetails() {
        return "{$this->student->getName()} needs to do {$this->details}";
    }
}

$assignment = new Assignment($someId);
$assignment->getDetails();

But for some reason I'm not allowed to access the assignment when it's protected. The only way to make it work is to make it public, but isn't that bad practice?

Speaking of bad practice, what about the whole approach of $student->assignment->getDetails(); would it be better practice to just make a getter for the assignment (i.e. $student->getAssignment()->getDetails()) ?

Thanks in advance


EDIT: Basically, I just want to know whether I can make $this->student->getName() work somehow, or whether it's better to use $this->getStudent()->getName().

The reason I'm confused about protected access is because I'm already in the assignment, and therefore I should have access to all properties, but I don't. I don't have access to $this->student, nor its methods, even though it's a property.

What's going on here?

Matt
  • 162
  • 1
  • 8
  • 1
    "would it be better practice to just make a getter for the assignment?" <- yes, that's how it's usually done. (And while you're at it, it would probably be better to also inject the assignment object as a constuctor parameter, instead of instantiating it inside it.) – Jeto Nov 14 '20 at 13:11
  • Also, "But for some reason I'm not allowed to access the assignment when it's protected." leads me to believe you probably should read up about [visibility](https://www.php.net/manual/en/language.oop5.visibility.php), as it's a very basic OOP concept. – Jeto Nov 14 '20 at 13:12
  • Ahh, I assumed that protected was like public, but not mutable. Like readable, but not writable. My bad :P – Matt Nov 14 '20 at 13:15
  • I just remembered that a while ago I wanted to set `$student` (as property of the assignment) private, but doing so made me lose access via `$this->student`. How come a property that's an object can't be accessed this way? – Matt Nov 14 '20 at 14:26
  • I don't see any such property in your sample code. You can access a private property from within its own class only. – Jeto Nov 14 '20 at 14:35
  • `return "$this->student->getName() needs to do $this->details";` <- what is this? And what makes you think you don't have access to `$this->student`, exactly? – Jeto Nov 14 '20 at 14:46
  • **Fatal error:** Uncaught Error: Cannot access private property Assignment::$student – Matt Nov 14 '20 at 14:51
  • [This is your exact code (with dummy IDs).](https://3v4l.org/tWvr9) The error you're mentioning doesn't occur, but there's another one on that `getDetails()` method because it doesn't make sense. – Jeto Nov 14 '20 at 14:57
  • Ok. Added curly braces. But this is really off topic. `return "{$this->student->getName()} needs to do {$this->details}";` <- this is just and example. In reality, my actual code has nothing to do with assignments or students. I just don't want to dump 100s of lines of me formatting HTML inside a PHP method for orders where I need to output information about `$this->customer->getPhone()` and `$this->assignee->getProfilePicture()` etc. It's just faster to write a dummy code as a quick example to demonstrate the problem. Now, why can't I access `$this->student` in the example? – Matt Nov 14 '20 at 15:04
  • 1
    [You can (again, your exact code).](https://3v4l.org/AkksM) (some parts of the string are empty because the student's name/assignment details are empty, but there's no error). It's perfectly fine to provide minimal code btw, but it's got to at least showcase the actual use case you're having trouble with. – Jeto Nov 14 '20 at 15:08
  • 1
    Oh my gosh… the error was in a total different spot. The error was in another file all together where I was apparently trying to access it from the outside like `$order->customer->getPhone()` while the inside worked like normal with `$this->customer->getPhone()`. That's an awkward mistake… o.o – Anyway, thanks for your input. I think I wouldn't have realised this without your help. – Matt Nov 14 '20 at 15:17

0 Answers0