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;
}
?
Related
I am getting this error:
uncaught error call to undefine method
Vehicles::setPassengerSeats() in
C:\xampp\htdocs\practice\vehicle.php:91.
I also have added screenshot of the error. Kindly check and tell me how can i solve it ? I think i have problem with my sub class but i don't know where?
Here is my source code:
<?php
class Vehicles{
private $noOfVehicles;
private $color;
private $fuel;
private $speed;
public function getNoOfVehicles(){
return $this->noOfMobiles;
}
public function setNoOfVehicles($Vehicles){
$this->noOfMobiles = $Vehicles;
echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
}
public function getColor(){
return $this->color;
}
public function setColor($look){
$this->color = $look;
echo "</br>The Color of Vehicle is: ".$this->color."</br>";
}
public function getFuel(){
return $this->fuel;
}
public function setFuel($petrol){
$this->fuel = $petrol;
echo "</br>The fuel is: ".$this->color."</br>";
}
public function getSpeed(){
return $this->speed;
}
public function setSpeed($vehicleSpeed){
$this->speed = $vehicleSpeed;
echo "</br>The speed of vehicle is: ".$this->speed."</br>";
}
}
class PassengerVehicles extends Vehicles{
private $passengerSeats;
public function getPassengerSeats(){
return $this->passengerSeats;
}
public function setPassengerSeats($seats){
return $this->passengerSeats = $seats;
echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
}
}
class TransportationVehicles extends Vehicles{
private $noOfDoors;
private $loadCapacity;
public function getNoOfDoors(){
return $this->noOfDoors;
}
public function setNoOfDoors($doors){
return $this->noOfDoors = $doors;
echo "</br>The No of Doors are: ".$this->noOfDoors."</br>";
}
public function getLoadCapacity(){
return $this->loadCapacity;
}
public function setLoadCapacity($capacity){
return $this->loadCapacity = $capacity;
echo "The Load Capacity is: ".$this->loadCapacity."</br>";
}
}
$VehiclesObj = new Vehicles;
$VehiclesObj->setNoOfVehicles("15");
$VehiclesObj->setColor("Black");
$VehiclesObj->setFuel("5 Litre");
$VehiclesObj->setSpeed("120 km/h");
$VehiclesObj->setPassengerSeats("4");
$VehiclesObj->setNoOfDoors("4");
$VehiclesObj->setLoadCapacity("500 KG");
?>
You call method setPassengerSeats which is in another class not in Vehicles You should create instance first, then to call this method:
$passangerVehicle = new PassengerVehicles;
$passangerVehicle->setPassengerSeats("4");
You can't call child methods from a parent. You need to create an instance of the child to be able to call parent methods
class Vehicles{
private $noOfVehicles;
private $color;
private $fuel;
private $speed;
public function getNoOfVehicles(){
return $this->noOfMobiles;
}
public function setNoOfVehicles($Vehicles){
$this->noOfMobiles = $Vehicles;
echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
}
public function getColor(){
return $this->color;
}
public function setColor($look){
$this->color = $look;
echo "</br>The Color of Vehicle is: ".$this->color."</br>";
}
public function getFuel(){
return $this->fuel;
}
public function setFuel($petrol){
$this->fuel = $petrol;
echo "</br>The fuel is: ".$this->color."</br>";
}
public function getSpeed(){
return $this->speed;
}
public function setSpeed($vehicleSpeed){
$this->speed = $vehicleSpeed;
echo "</br>The speed of vehicle is: ".$this->speed."</br>";
}
}
class PassengerVehicles extends Vehicles{
private $passengerSeats;
public function getPassengerSeats(){
return $this->passengerSeats;
}
public function setPassengerSeats($seats){
return $this->passengerSeats = $seats;
echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
}
}
class TransportationVehicles extends Vehicles{
private $noOfDoors;
private $loadCapacity;
public function getNoOfDoors(){
return $this->noOfDoors;
}
public function setNoOfDoors($doors){
$this->noOfDoors = $doors;
echo "</br>The No of Doors are: {$this->noOfDoors}</br>";
return $this->noOfDoors;
}
public function getLoadCapacity(){
return $this->loadCapacity;
}
public function setLoadCapacity($capacity){
return $this->loadCapacity = $capacity;
echo "The Load Capacity is: ".$this->loadCapacity."</br>";
}
}
$truck = new TransportationVehicles();
$truck->setNoOfVehicles("15");
$truck->setColor("Black");
$truck->setFuel("5 Litre");
$truck->setSpeed("120 km/h");
$truck->setNoOfDoors("4");
$truck->setLoadCapacity("500 KG");
$taxi = (new PassengerVehicles())->setPassengerSeats('4');
In this case, you will have two instances of the Vehicles class + own child.
The first instance is related to the Vehicle itself + transport properties like $noOfDoorsand $loadCapacity - a truck for example.
The second is an instance of a passenger based vehicle - taxi for example.
And you tried to get passengers option of a taxi from a bus.
I have two php classes in two separate files:
File Name : ld.php
<?php
class LandDetail_Model{
public function __construct() {}
private $id;
public $pId;
private $bigha;
private $katha;
function setId($id) { $this->id = $id; }
function getId() { return $this->id; }
function setPId($pId) { $this->pId = $pId; }
function getPId() { return $this->pId; }
function setBigha($bigha) { $this->bigha = $bigha; }
function getBigha() { return $this->bigha; }
function setKatha($katha) { $this->katha = $katha; }
?>
File Name :Land_Detail.php
<?php
require_once '../models/ld.php';
class LandDetail extends LandDetail_Model{
public function __construct() {
parent::__construct();
}
public function DoSomething(){
echo "This is a value from Parent".$this->getPId();
}
}
?>
Now in SomeFile.php I am doing something like this.
<?php
include 'Land_Detail.php';
$ld = new LandDetail();
$ld->setPId(10001);
$ld->DoSomething();
?>
Why $this->getPId() is always returning null value? What is wrong with my code here? What is the correct way to extend a class in php from different file?
Why you write:
$ld->setPId = 10001;
Instead of
$ld->setPId(10001);
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
I have a person class with a $name property and an officer class with an $officer_name property. I need to be able to get the $name property from the person class to the officer class. In this case it should have an output of "Major Blake". Do I need to make an instance of the officer class for me to be able to echo this out?
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct() {
echo $officer_name = $this->give_rank() . parent::get_name();
}
}
$iv_person = new person("Blake");
Please try this code :
your class :
<?php
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct($fv_name) {
parent::__construct($fv_name);
}
function output(){
return $this->give_rank().parent::get_name();
}
}
your call :
$iv_person = new officer("Blake");
echo $iv_person->output();
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();