Cole PHP Sample Code

CCMars · March 04, 2022
<?php
// Cole Class
namespace Marshall;

class Cole {
	const NAME_FIRST = "Cole";
	const NAME_LAST = "Marshall";
	private string $profession;
	private array $specialties;

	function __construct(string $initialProfession = "Interactive Developer & Designer") {
		$this->profession = $initialProfession;
		$this->specialties = [
			"HTML", "CSS", "JavaScript", "Sass", "Vue.js", "jQuery",
			"cross-browser compatibility", "mobile/responsive design", "search engine optimization",
			"PHP", "MySQL", "REST", "XML", "JSON", "Git", "automation",
			"Photoshop", "Illustrator", "XD", "After Effects", "Graphic Design", "Typography",
			"User Interface (UI) Design", "User Experience (UX) Design", "Motion Graphics"
		];
	}

	public function getFullName():string {
		return self::NAME_FIRST . ' ' . self::NAME_LAST;
	}

	public function getProfession():string {
		return $this->profession;
	}

	public function setProfession(string $newProfession):string {
		return $this->profession = $newProfession;
	}

	public function getSpecialties():array {
		return $this->specialties;
	}

	public function setSpecialties(array $newSpecialties):array {
		return $this->specialties = $newSpecialties;
	}

	public function getReadableDetails():string {
		$readable = $this->getFullName() . " is " . (preg_match('/^[aeiou]/i',$this->getProfession())?"an ":"a ") . $this->getProfession() . " who specializes in ";
		for ($i = 0; $i < count($this->getSpecialties()); $i++) {
			$readable .= $this->getSpecialties()[$i];
			if ($i == count($this->getSpecialties()) - 2) {
				$readable .= (count($this->getSpecialties()) > 2?",":'') . " and ";
			} else if ($i == count($this->getSpecialties()) - 1) {
				$readable .= ".";
			} else {
				$readable .= ", ";
			}
		}
		return $readable;
	}
}

// Instantiate Cole
$cole = new \Marshall\Cole();

// Echo Cole's Profession and Specialties
echo $cole->getReadableDetails();
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.