PHP access methods from parent class - php

Hello guys I'm new to PHP's OOP so I need a little help from my test scripts.
This is what I've tried so far:
index.php
<?php
include("shared.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Car Details</title>
</head>
<body>
<?php
$car1 = new Car("Audi");
echo $car1->showCarDetails();
?>
</body>
</html>
car.php
<?php
class Car extends CarDetails {
public $name;
public $color = "Freaking Sexy White";
public $price = "PHP 4,000,000.00";
public function _construct($name) {
$this->setName($name);
$this->getColor();
$this->getPrice();
}
public function setName($name) {
$this->name = $name;
}
public function setColor($color) {
$this->color = $color;
}
public function setPrice($price) {
$this->price = $price;
}
public function getName() {
return $this->name;
}
public function getColor() {
return $this->color;
}
public function getPrice() {
return $this->price;
}
public function showCarDetails() {
print nl2br("I have an awesome car. Below are the details :)\r\n".
"Brand: " . $this->getName() . "\r\n" .
"Model: " . parent::getModel(). "\r\n" .
"Color: " . $this->getColor() . "\r\n" .
"Price: " . $this->getPrice()
);
}
}
?>
cardetails.php
<?php
class CarDetails {
public $model = "A7 Sportback";
public $engine = "FSI technology";
public function setModel($model) {
$this->model = $model;
}
public function getModel() {
return $this->model;
}
public function setEngine($engine) {
$this->engine;
}
public function getEngine() {
return $this->getEngine;
}
}
?>
shared.php
<?php
function __autoload($className)
{
//echo "We are requesting the " . $className . " class";
if(file_exists($className . ".php"))
{
require_once($className . ".php");
//echo "The " . $className . " has been included";
}
}
?>
I want to access the method from my parent class which is CarDetails.php, getModel() and getEngine(). But I don't know how to do that, and also what I have declared in the constructor of Car.php in my index.php is not found.
The output:
Notice: Object of class Car could not be converted to int in C:\xampp\htdocs\oop\cardetails.php on line 13
I have an awesome car. Below are the details :)
Brand:
Model: 1
Color: Freaking Sexy White
Price: PHP 4,000,000.00
But my intended output should be:
I have an awesome car. Below are the details :)
Brand: Audi
Model: A7 Sportback
Color: Freaking Sexy White
Price: PHP 4,000,000.00
What is the problem in my code? Any ideas? I'd truly appreciate your help. Thanks.
UPDATE:
I can now access the methods from my parent class. But the problem is, I'm not seeing anything that I declared in my constructor.
Brand: __
Where it should be:
Brand: Audi
Since I passed in "Audi" in index.php

You have a couple of typos in cardetails.php:
public function setEngine($engine) {
$this->engine;
}
public function getEngine() {
return $this->getEngine;
}
should instead be
public function setEngine($engine) {
$this->engine = $engine;
}
public function getEngine() {
return $this->engine;
}
Also, in car.php:
public function _construct($name) {
should be
public function __construct($name) {
I believe that's causing the weirdness you're seeing.

So I've made a few changes here, the whole point behind extending your class is so that your child class HAS the public/protected functionality that the parent class had.
This would mean that your child class Car shouldn't need to call parents when accessing the getModel or any other functions.
You can see the code changes run live here, https://ideone.com/vCfxYQ
<?php
class Car extends CarDetails {
public $name;
public $color = "Freaking Sexy White";
public $price = "PHP 4,000,000.00";
public function __construct($name) {
$this->setName($name);
$this->getColor();
$this->getPrice();
}
public function setName($name) {
$this->name = $name;
}
public function setColor($color) {
$this->color = $color;
}
public function setPrice($price) {
$this->price = $price;
}
public function getName() {
return $this->name;
}
public function getColor() {
return $this->color;
}
public function getPrice() {
return $this->price;
}
public function showCarDetails() {
print nl2br("I have an awesome car. Below are the details :)\r\n".
"Brand: " . $this->getName() . "\r\n" .
"Model: " . $this->getModel(). "\r\n" .
"Color: " . $this->getColor() . "\r\n" .
"Price: " . $this->getPrice()
);
}
}
class CarDetails {
public $model = "A7 Sportback";
public $engine = "FSI technology";
public function __construct() {
}
public function setModel($model) {
$this->model = $model;
}
public function getModel() {
return $this->model;
}
public function setEngine($engine) {
$this->engine = $engine;
}
public function getEngine() {
return $this->getEngine;
}
}
$car1 = new Car("Audi");
echo $car1->showCarDetails();

Related

PHP interface coding

I am having a PHP interface coding problem. I took java code from the book “Head First Design Patterns and converted it to the code below. I am using MAMP/ PHP 5.6.2 and NetBeans 8.1.
I am trying to implement an interface “TestInterface” in the Menu class that extends an abstract class (MenuComponent). The Menu class will not start with the “TestInterface” implementation. The code runs when I comment out “TestInterface" in the Menu class declaration as the code below. And while “TestInterface” is commented out, PHP throws no errors even when declaring the interface and keeping the interface function as a Menu member function. I have successfully ran simpler code while extending and implementing at the same time using the same platform as stated above. Because of success with simpler code, I believe there is structural or syntax error in my code below. I hoping that someone can help me find what I am doing wrong. Thanks in advance.
<?php
$run = new myclass;
$run->main();
class myclass {
private $pancakeHouseMenu;
private $allMenus;
private $waitress;
public function main(){
echo "<br />hi main!<br />";
$this->pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");
$this->allMenus = new Menu("ALL MENUS", "All menus combind");
$this->allMenus->add($this->pancakeHouseMenu);
$this->pancakeHouseMenu->add(new MenuItem(
"Regular Pancake Breakfast",
"Pancakes with eggs and sausage"));
$this->waitress = new Waitress($this->allMenus);
$this->waitress->printMenu();
}
}
interface TestInterface {
public function interfaceTest();
}
abstract class MenuComponent {
public function add(MenuComponent $newMenuComponent) {
throw new InvalidArgumentException("Exception thrown");
}
public function getName() {
throw new InvalidArgumentException("Exception thrown");
}
public function getDescription() {
throw new InvalidArgumentException("Exception thrown");
}
public function printOut() {
throw new InvalidArgumentException("Exception thrown");
}
}
class Waitress {
private $allMenus;
public function __construct(MenuComponent $allMenus) {
$this->allMenus = $allMenus;
$this->allMenus->add($allMenus);
}
public function printMenu() {
$this->allMenus->printOut();
}
}
class MenuItem extends MenuComponent {
private $name;
private $description;
public function __construct($name, $description) {
$this->name = $name;
$this->description = $description;
}
public function getName() {
return $this->name;
}
public function getDescription() {
return $this->description;
}
public function printOut() {
print(" " . $this->getName());
print(" -- " . $this->getDescription());
}
}
class Menu extends MenuComponent /*** implements TestInterface ***/ {
private $menuComponents = array();
private $name;
private $description;
// private $testVar;
public function __construct($name, $description) {
$this->name = $name;
$this->description = $description;
$this->testVar = "Interface test succeeded";
}
public function interfaceTest(){
return $this->testVar;
}
public function add(MenuComponent $newMenuComponent) {
array_push($this->menuComponents, $newMenuComponent);
}
public function getName() {
return $this->name;
}
public function getDescription() {
return $this->description;
}
public function printOut() {
print("<br />" . $this->getName());
print(", " . $this->getDescription());
print("<br />---------------------");
print("<br />Testing interface var: ". $this->interfaceTest());
}
}
?>
In your code you create an object above the declaration of your classes. This seems to be ok if your classes do not implement any interfaces. Since your class menu does implement the interface TestInterface, PHP does not accept your object instantiation before the declaration of your classes.
The solution is quite simple, place your object creation of myclass below the object declaration:
<?php
class myclass {
private $pancakeHouseMenu;
private $allMenus;
private $waitress;
...
public function getDescription() {
return $this->description;
}
public function printOut() {
print("<br />" . $this->getName());
print(", " . $this->getDescription());
print("<br />---------------------");
print("<br />Testing interface var: ". $this->interfaceTest());
}
}
$run = new myclass;
$run->main();
?>

Managing collection of items through adding/removing

I want have an in-memory data structure to be able to add or remove an item (in this instance a student) into some sort of table (just like a shopping cart) from the collection class I have created. At the moment, it just displays students. For instance, if I click add student, it will pop up below, and I can delete this student from below also.
How I could implement this?
Here is my Member.php class
<?php
class Member {
private $name;
private $age;
private $gender;
private $course;
public function __construct($name,$age, $gender, $course){
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
$this->course = $course;
}
public function setName($name) { //Sets the age value
$this->name = $name;
}
public function setAge($age) { //Sets the age value
$this->age = $age;
}
public function setGender($gender) { //Sets the gender value
$this->gender = $gender;
}
public function setCourse ($course) {
$this->course = $course;
}
public function getName() { //Gets the name value
return $this->name;
}
public function getAge() { //Gets the age value
return $this->age;
}
public function getGender() { //Gets the gender value
return $this->gender;
}
public function getCourse() {
return $this->course;
}
}
?>
Here is my ObjectCollection.php
<?php
class ObjectCollection
{
//This is an array to hold line items
private $items_array ;
private $itemCounter; //Count the number of items
public function __construct() {
//Create an array object to hold line items
$this->items_array = array();
$this->itemCounter=0;
}
public function getItemCount(){
return $this->itemCounter;
}
public function addItem($item) {
$this->itemCounter++;
$this->items_array[] = $item;
}
public function getItem($index) {
return $this->items_array[$index];
}
}
?>
And finally displaying this through testcollection.php
<?php
$ObjColl = new ObjectCollection();
$member1 = new Member("Jessica Davidson", 21, "Female", "Computing");
$ObjColl->addItem($member1);
$member2 = new Member("Lucy Barnes", 22, "Female", "History");
$ObjColl->addItem($member2);
$member3 = new Member("Mark Smith", 24, "Male", "Social Science");
$ObjColl->addItem($member3);
for($i = 0;$i < $ObjColl->getItemCount();$i++){
$item = $ObjColl->getItem($i);
if ($item instanceof Member) {
print "<br> University Member: ";
}
print "Name: " . $item->getName();
print ". Age: " . $item->getAge();
print ". Gender: " . $item->getGender();
print ". Enrolled on: " .$item->getCourse() . " course<br>";
}
?>
At first if your ObjectCollection must collect only objects of Member class, use parameter type declaration. It’s good practice in OOP.
public function addItem(Member $item)
At second if you want work with ObjectCollection like with array, implement ArrayAccess and Iterator interfaces.
Example
<?php
class Member{
private $__name;
public function __construct($name){
$this->__name = $name;
}
public function getName(){
return $this->__name;
}
}
class MemberCollection implements ArrayAccess, Iterator{
private $__Collection = [];
private $__position = 0;
public function __construct(){
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->__Collection[] = $value;
} else {
$this->__Collection[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->__Collection[$offset]);
}
public function offsetUnset($offset) {
unset($this->__Collection[$offset]);
}
public function offsetGet($offset) {
return isset($this->__Collection[$offset]) ? $this->__Collection[$offset] : null;
}
function rewind() {
$this->__position = 0;
}
function current() {
return $this->__Collection[$this->__position];
}
function key() {
return $this->__position;
}
function next() {
++$this->__position;
}
function valid() {
return isset($this->__Collection[$this->__position]);
}
public function addItem(Member $Member){
$this->offsetSet(null, $Member);
}
}
$MC = new MemberCollection();
$Member1 = new Member('Name 1');
$Member2 = new Member('Name 2');
$MC->addItem($Member1);
$MC->addItem($Member2);
foreach ($MC as $Member){
echo '<br>' . $MC->key() . ':<br>';
var_dump($Member->getName());
}
unset($MC[0]); //Delete member from collection
?>

Removing duplicate-like function which uses static to denote the class name in PHP

Is there a way to get rid of the getCopy() method in the derived class HelloAndGoodbye, given that it looks the same as the getCopy() in the base class Hello?
And what is more, what is there an efficient way to achieve this?
(the only difference between the two functions is that in the baseclass 'static' refers to 'Hello' and in the derived class 'static' refers to 'HelloAndGoodbye; as for the variables contained therein they can be easily renamed so that they are the same in both functions).
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$helloCopy = static::createStub();
$this->doCopy($helloCopy);
return $helloCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function getCopy() {
$helloAndGoodbyeCopy = static::createStub();
$this->doCopy($helloAndGoodbyeCopy);
return $helloAndGoodbyeCopy;
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane
I found a solution to the problem at hand by means of using the __CLASS__ PHP constant which correspond to the name of the class within which it appears. This allowed me to get rid of the pseudo-duplicate getCopy() method in the derived class, while still allowing getCopy() to work fine on both:
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$class = __CLASS;
$instanceCopy = $class::createStub();
$this->doCopy($instanceCopy);
return $instanceCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane

Method chaining

class A {
public function model($name) {
if (file_exists($name.'.php')) {
require $name.'.php';
$this->$name = new $name();
}
}
}
class C extends A {
function __construct() {
$this->load = $this;
$this->load->model('test');
$this->test->say();
}
}
$Controller = new C();
I want to create a simple code igniter like loader class. Is there a proper way for doing this technique?
You would use Fluent Interface pattern.
<?php
class Employee
{
public $name;
public $surName;
public $salary;
public function setName($name)
{
$this->name = $name;
return $this;
}
public function setSurname($surname)
{
$this->surName = $surname;
return $this;
}
public function setSalary($salary)
{
$this->salary = $salary;
return $this;
}
public function __toString()
{
$employeeInfo = 'Name: ' . $this->name . PHP_EOL;
$employeeInfo .= 'Surname: ' . $this->surName . PHP_EOL;
$employeeInfo .= 'Salary: ' . $this->salary . PHP_EOL;
return $employeeInfo;
}
}
# Create a new instance of the Employee class:
$employee = new Employee();
# Employee Tom Smith has a salary of 100:
echo $employee->setName('Tom')
->setSurname('Smith')
->setSalary('100');
# Display:
# Name: Tom
# Surname: Smith
# Salary: 100

How to color text

I have this code and the sad thing is i can't change anything or add outside the class STUDENT. I can modify only inside STUDENT and i can't alter the private fields. However I need to display the value of the field $nume in red color. Ideas?
class STUDENT {
private $nume,$prenume;
// Constructor
public function __construct($nume , $prenume){
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString(){
return $this->nume.".".$this->prenume;
}
}
$student = new STUDENT("mr","Jack");
echo "student: ". $student ."<hr/>";
You can make the properties public so you can access them from the outside like this:
class STUDENT
{
public $nume;
public $prenume;
// Constructor
public function __construct($nume , $prenume)
{
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString()
{
return $this->nume.".".$this->prenume;
}
}
$student = new STUDENT("mr","Jack");
echo "<span style='color:red'>student: ". $student->nume ."</span><hr/>";
Or if you need to keep the private you can create a function inside the class to output it:
class STUDENT
{
private $nume;
private $prenume;
// Constructor
public function __construct($nume , $prenume)
{
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString()
{
return $this->nume.".".$this->prenume;
}
public function displayNume()
{
echo "<span style='color:red'>student: ". $this->nume ."</span><hr/>";
}
}
Which you can then access like this:
$student = new STUDENT("mr","Jack");
$student->displayNume();
Have you tried...
public function __toString(){
$red = '<span style="color: red;">' . $this->nume . '</span>';
return $red.".".$this->prenume;
}
?

Categories