I have the following code
<?php
class ingredient {
private $calories;
public function __construct() {
$this->calories = 100;
}
public function get_calories() {
return $this->calories;
}
}
class flour extends ingredient {
private $texture;
public function __construct(texture) {
$this->texture = $texture;
}
public function get_texture() {
return $this->texture;
}
}
$plain_flour = new flour("Grainy");
My question is, does plain_flour have the attribute calories initialized already? Can I call get_calories on plain_flour? If no, how can I make it work so flour has the calories attribute ready to go and I can use the get_calories function
Since the $calories element is part of the ingredient class and is a private variable.
To access the variable from the flour class, you can call the parent's __construct method.
See this:
class ingredient {
private $calories;
public function __construct() {
$this->calories = 100;
}
public function get_calories() {
return $this->calories;
}
}
class flour extends ingredient {
private $texture;
public function __construct($texture) {
$this->texture = $texture;
parent::__construct();
}
public function get_texture() {
return $this->texture;
}
}
$plain_flour = new flour("Grainy");
echo $plain_flour->get_calories();
Related
I have a problem with an error I am getting that says:
Class Car contains 1 abstract method and must therefore be decla
red abstract or implement the remaining methods (Car::accelerate) in C:\xampp
\htdocs\php\learn_php_oop\Car.php on line 58.
This is the code in two files I am using:
Car.php
<?php
/**
* represents generic properties and methods for any type of car
*/
class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Sport_car.php
<?php
include ('Car.php');
/**
* represents sport cars
*/
class Sport_car extends Car
{
public function accelerate()
{
$this->accelerate = 5;
}
}
?>
I have spent some time trying to figure out why this is happening but I just do not know why? Please help.
It's an OOP problem, in your case you must declare your Car Object as Abstract like this :
<?php
/**
* represents generic properties and methods for any type of car
*/
abstract class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Explanations :
A class wich is extended with at least one abstract method in it has to be defined as abstract itself, otherwise you'll get an error
If you have an abstract class, is there a way to keep a counter inside the abstract class to count how many times it's used?
Because if I do this:
abstract class abstractClassName {
private $counter = 0;
public function __construct() {
$this->counter++;
}
public function outputCounter() {
echo $this->counter;
}
}
class someExtension extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
class someExtensionTwo extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
and then
$class = new someExtension;
$class->outputCounter();
$class2 = new someExtensionTwo;
$class2->outputCounter();
I get 1 twice, yet I was expecting to get 1 and then 2 on the last call, and I'm getting confused over how it's all meant to work.
Edit: changed code to reflect the real code more :)
If you are wishing to keep track of how many time the class AbstractClassName has been instantiated, you will need to make use of static variables -- so that the value of $this->counter is persistent, for example:
abstract class abstractClassName
{
private static $counter = 0;
public function __construct() {
self::$counter++;
}
public function outputCounter() {
echo self::$counter;
}
}
class someExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
class someOtherExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
$class = new someExtension;
$class->outputCounter();
$class2 = new someOtherExtension;
$class2->outputCounter();
Which would result in an output of: 1 & 2.
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
public $publicc = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}
$obj = new C();
$obj->method1();
But php throws error echo $this->publicc + parent::$publicc. I just want to get parent class $publicc property directly that has value 2, Without using any accessor method. Is there a way to do this in php?
It depends on what publicc exactly holds but a constant might suit your needs?
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
const PUBLICC = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
const PUBLICC = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo self::PUBLICC + parent::PUBLICC; // error for using parent::PUBLICC
}
}
$obj = new C();
$obj->method1();
I think you want a static property. If it is a property you want to access without an instance, that usually indicates a candidate for a static variable.
abstract class B implements A
{
protected static $publicc = 2;
...
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}
I have these related classes:
class cars {
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
public function getParams()
{
return $this->params;
}
}
$cars = new cars();
class bmw extends cars {
private static $_instance = null;
protected $params;
function __construct()
{
$this->params['param'] = 'foo';
}
public static function init()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
}
$cars->addCar( 'bmw', bmw::init() );
Basically i need to access all child classes from parent class. And use methods defined in parent class on those defined child classes. Parent class should not be modified when adding new child classes.
In the end this should work like this:
foreach( $cars->getCars() as $car )
{
foreach( $car->getParams() as $key => $param )
echo "$key = $param";
}
What is the proper way to do this?
It's really difficult to provide an help since it's not so clear what you're trying to achieve.
It seems to me that you need Registry Class (carDealer), an abstract class with common (for each child) methods and a child (Bmw) of this.
So, something like:
// You seems to need what is called sometimes a Registry.
// Something which deal with keeping and delivering a group of 'related' classes, as a register.
class CarsDealer
{
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
}
// then you need a basic contract for each concrete classes
// that will have the same nature and so will extend it
abstract class Car
{
protected $params;
public function getParams()
{
return $this->params;
}
}
// finally the concrete class
class Bmw extends Car
{
public function __construct($params = null)
{
$this->params['param'] = $params;
}
}
$carsDealer = new CarsDealer();
$carsDealer->addCar('bmw', new Bmw('foo'));
foreach ($carsDealer->getCars() as $car)
{
foreach ($car->getParams() as $key => $param) {
echo "$key = $param";
}
}
Please pay attention to some basic rules/good practices/conventions:
class naming, always capitalized
Responsibilities (a class Bmw shouldn't have a method getCars, at least not in this example)
Visibility of method, parameters
http://www.php-fig.org/psr/psr-1/
http://www.php-fig.org/psr/psr-2/
Just one another approach, if you just need get this 'params' :-)
class cars {
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
public function getParams($obj)
{
return $obj->params;
}
}
$cars = new cars();
class bmw extends cars {
private static $_instance = null;
protected $params;
function __construct()
{
$this->params['param'] = 'foo';
}
public static function init()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
}
$cars->addCar( 'bmw', bmw::init() );
print_r( $cars->getParams($cars->getCar('bmw')));
I have two class es
class Pet {
public $pet = null;
public function setPet(){}
public function getPet(){}
}
and
class B {
public $cat = 'cat';
public $dog = 'bog';
public function cat()
{
$pet = new Pet();
$pet->pet = $this->cat;
}
public function dog()
{
$pet = new Pet();
$pet->pet= $this->dog;
}
}
Can I get this:
$pet = new Pet();
$pet->setPet()->dog();
$pet->getPet(); //dog
I don't believe you can. You could make class B extends Pet. That will allow you to call the functions from the class Pet. Read up on object inheritance, that might help! http://php.net/manual/en/language.oop5.inheritance.php
You can simply extend Class Pet on Class B to call functions from the Pet class. So Class B inherits the functions of Pet.
Class B extends Pet {
// class B functions here...
}
I laughed while I'm writing down my code here..
<?php
class Pet {
public $name;
public function setName($string) {
$this->name = $string;
}
public function getName() {
return $this->name;
}
}
class Dog extends Pet {
public function bark() {
echo "Arf arf!";
}
}
class Cat extends Pet {
public function meow() {
echo "Meoooww~ purrr~";
}
}
$dog = new Dog();
$dog->setName("Jacob");
$dog->bark(); //Arf arf!
echo "Good job, ".$dog->getName()."!"; //Good job, Jacob!
?>
sir you cant call $pet->setPet()->dog() with ->dog() since setPet() is a function and not an object.. just like they said, the right thing to do with your code is to extend it as a super class and declare a Dog Class as the child class..
My variant
class Pet {
public $pet = null;
public function setPet($pet = null)
{
if (is_null($pet)) {
return new B($this);
} else {
$this->pet = $pet;
return $this;
}
}
public function getPet()
{
return $this->pet;
}
}
class B {
protected $pet = null;
protected $cat = 'cat';
protected $dog = 'bog';
public function __construct(Pet $pet)
{
$this->pet = $pet;
}
public function cat()
{
$this->pet->setPet($this->cat);
return $this->pet;
}
public function dog()
{
$this->pet->setPet($this->dog);
return $this->pet;
}
}
$pet = new Pet();
$pet->setPet()->cat();
var_dump($pet->getPet());