Recursion Issue

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:

  1. How would I write this without having the recursion happen?
  2. Is the recursion bad in this case?
  3. 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


recursion.jpg

To me using abstract classes would be more versatile.

I was just goofing around and this is far from perfect, but it shows the power of using abstraction. You can incorporated databases, user’s input or what have you.

Here’s the script I was fooling around with:

[php]<?php

abstract class PersonClass
{
// Our abstract method only needs to define the required arguments
abstract protected function person();
abstract protected function technician();
abstract protected function business();

// Common method
public function printOut() {
	print 'Customer ' . $this->person() . ' is talking to ' . $this->technician() . ' a technician at ' . $this->business() . "! <br>\n";
}

}

class CustomerClass extends PersonClass
{
public $person;
public $technician;
public $company;

public function __construct($person = 'Bill Gates', $technician = 'Steve Wozniak', $company = 'Apple Inc.') {
	$this->person = $person;
	$this->technician = $technician;
	$this->company = $company;
}

public function update($person, $technician, $company) {
	self::__construct($person, $technician, $company);
}

	// Our child class may define optional arguments not in the parent's signature
public function technician() {
	return $this->technician;
}
  // Our child class may define optional arguments not in the parent's signature
public function business() {
	return $this->company;
}
// Our child class may define optional arguments not in the parent's signature
public function person($address=NULL) {
	if ($address) {
		return $this->person . ' ' . $address;
	} else {
		return $this->person;
	}
}

}

$class = new CustomerClass();
$class->printOut();
$class->update(‘Scrfix’, ‘Strider64’, ‘PHPHelp.com’);
$class->printOut();

$customer = new CustomerClass(‘Justin Verlander’, ‘Jack Morris’, ‘Detroit Tigers’);
$customer->printOut();[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service