Issue:
The issue I am having is recursion as stated in the subject line. This is probably just badly written.
The code is below and the attachment shows a dump output using kint.
Notes:
This code is merely an attempt to me learning how to name and write classes and have those classes interact with each other properly.
The notes are just there so you can see where I was going with this.
Questions:
- How would I write this without having the recursion happen?
- Is the recursion bad in this case?
- Would you be able to provide an example or explain in detail (non technical) what I am doing wrong or how to do it better?
[php]<?php
error_reporting(E_ALL);
// require ‘…/…/…/classes/kint-0.9/Kint.class.php’; // Make dumps easier to read.
/**
An attempt to work with messaging between PHP Classes.
*/
/*
Instructions on what should happen when a customer hires a company to work on their PC.
1. Person walks into a business with their computer.
2. Person speaks with the technician from the Business.
3. Person hires the business to fix their computer.
4. Person becomes a Customer of the business.
Notes:
Person should have a name required.
Person does not depend on business.
Business does not depend on person.
Becoming a Customer depends on both the business and person.
Person becomes customer and gets added to Customer list for the business.
*/
class Person {
protected $customerName;
protected $businessName;
public function __construct($name) {
$this->customerName = $name;
}
public function becomes(Customer $customerName, Business $business) {
$this->businessName = $business;
$this->businessName->adds($customerName);
}
}
class Business {
protected $customers = [];
protected $businessName;
public function __construct($businessName) {
$this->businessName = $businessName;
}
public function adds(Customer $customerName) {
$this->customers[] = $customerName;
}
}
class Customer {
protected $customer;
public function __construct(Person $person, Business $businessName) {
$this->customer = $person;
}
}
$person = new Person(‘Kammi Iungano’);
$business = new Business(‘Spectacular Computer Repair’);
$customer = new Customer($person, $business);
$person->becomes($customer, $business);
// d($customer); // dump $customer using kint.
var_dump($customer);
[/php]
Thanks,
Wayne