PHP Help with Call to a member function add() on a non-object

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/

Kind of hard to debug something if you’re purposely causing an error. Debugging 101, have the actual error cause the problem that way others can help you better.

Hi Strider,

Thanks for the response. I agree with you however in this particular instance the error remains regardless of what was put there. If that particular error was repaired “Call to a member function add() on a non-object” then another error would pop-up talking about it has a wrong object or something or “PHP Catchable fatal error: Object of class Business could not be converted to string”; great, question resolved!

The fact that I didn’t have $name in there vs. $specrep in there makes no difference at all in this particular case. It was not the cause of the issue so it is not any more difficult to debug this particular error regardless of what was there.

The only reason I mentioned it was to save time because I simply didn’t want people focusing on the fact that during the debug process I commented out the add method to see if that was the issue and replaced the one object with one that I verified was indeed an object with var_dump just to try to get rid of the error because the message I was receiving was Call to member function on a “non-object”. However as my update discusses thanks to the debugging tutorial on this site I was able to discover that was indeed not the actual error. Now I am working on a solution so Thank you to whomever put that tutorial out there. That post should be sticky in my opinion. It was extremely helpful.

Guys, I want to say Thanks Again. That debugging tutorial was amazing and helped out tremendously. I don’t have the code working currently however I have went back to the drawing board and started from scratch. A whole 40 lines of code erased… :stuck_out_tongue:

I have been trying to pick up PHP Objects for quite some time in my very limited spare time and I could not find any good “How to Debug” tutorials. I did find a lot of piece meal stuff that says, do this… and then something else says don’t do this… ahhhhhhhhhh and then you forget what both of them said because you cannot get back to it for 5 months. That tutorial should be sticky or at minimal a link should be provided in the actual instructions so someone doesn’t have to search for it.

I now have a whole new world of errors that are much easier to understand and it is helping me in my line of thinking on creating and working with objects. I have a ton of work to do still but at least I feel I am on the right track.

Sponsor our Newsletter | Privacy Policy | Terms of Service