Question: What is the problem that I apparently cannot pass an object through to this method?
Code: See Below
Issue: Line 23 $this->business->add($specrep); returns Call to a member function add() on a non-object when indeed there is an object in there albeit not a correct object but still an object.
Goal: To successfully have 2 or more classes talking back and forth between each other and to understand how to accomplish this myself.
Notes:
[ul][li]The code is just for me to learn from. It is not anything serious. I created it because I am watching a tutorial about messaging 101 on https://laracasts.com/series/object-oriented-bootcamp-in-php/episodes/5[/li]
[li]I know that line 23 on the code is not correct. I made it that way trying to ensure that I indeed did have an object in that request. I left it that way so you could see that an object is passed through to the call. I can make his stuff work however obviously don’t understand this well enough to make my own work.[/li]
[li]I am aware that the add function on line 33-35 is commented out. The error in question is there whether this is commented out or not.[/li]
[li]When I var_dump $specrep on line 41 it definitely returns an object so I should be passing an object through when calling the method.[/li]
[li]I have modified this multiple times and rewrote multiple times and I just am not sure what I am doing wrong.[/li][/ul]
Code:
[php]<?php
/**
An attempt to work with messaging between PHP Classes.
*/
/*
Instructions on what should happen when a customer hires an I.T. person
1. Customer walks into computer store with their computer.
2. Customer speaks with technician for the Business.
3. Customer hires the technician to fix their computer.
4. Computer gets assigned to a technician.
*/
class Customer {
private $name;
protected $specrep;
public function __construct($name) {
$this->name = $name;
}
public function hires(Business $specrep) {
// Business wants to add the customer to their customer list!
$this->business->add($specrep);
}
}
class Business {
private $customers = [];
private $companyName;
public function __construct($companyName) {
$this->companyName = $companyName;
}
// public function add(Customer $customers) {
// $this->customers[] = $customers;
// }
}
$customer = new Customer(‘Kammi Iungano’);
$specrep = new Business(‘Spectacular Computer Repair’);
var_dump($customer);
var_dump($specrep);
$customer->hires($specrep);
[/php]
UPDATE:
I just read the debugging tutorial on here so I am trying that as well which just revealed: PHP Notice: Undefined property: Customer::$business on line 23
This made me realize that my class has no idea who the business class is because I haven’t told it. The error I was look at mentioned above isn’t the actual error. Thank you to the debugging tutorial for that. Since that is my error I can see that I haven’t properly told the Customer Class about the Business class which is why trying to utilize a function from the business class is failing.
By the way, To the MODS: in the sticky guidelines it says, in bold, read our debugging tutorial however it is not a link. There should be a link to the debugging tutorial because it is a great tutorial and thank you to the person that said put error_reporting(E_ALL); at the top of all of your documents.
Debugging Tutorial:
http://www.phphelp.com/forum/the-occasional-tutorial/debugging/