I am experimenting with php class inheritance, I wrote the below code and got an uncaught error that the User class is not found. Please any help about where am going wrong will be fine.
the first block is the User class
<?php
class User{
private $name;
private $age;
public function _construct($name, $age){
$this->name = $name;
$this->age = $age;
}
}
?>
below is the customer class that inherit the user class.
<?php
class Customer extends User{
private $balance;
public function __construct($name, $age, $balace) {
parent::_construct($name, $age);
$this->balance = $balace;
}
public function pay($amount){
return $this->name ." paid $".$amount;
}
public function getBalance(){
return $this->balance;
}
}
$customer = new Customer("Matt", 23, 500);
echo $customer->getBalance();
I have checked your code by implementing it in a single file. It works fine.
If you are having 2 files for both classes, you need to include the User class file into the Customer class file like below.
require_once('User.php');
Related
i want to show a name.name may be in uppercase or in lowercase this will be depend on which class i will pass.i have to approach one is using class and second is interface which one is better and why ?
Solution using Class
class User{
}
class Iuser extends User{
private $name;
function __construct($name){
$this->name = $name;
}
function getName(){
return strtoupper($this->name);
}
}
class Wuser extends User{
private $name;
function __construct($name){
$this->name = $name;
}
function getName(){
return strtolower($this->name);
}
}
class Name{
public $u;
function __construct(User $u){
$this->u = $u;
}
function getName(){
$name = $this->u->getName();
return "Hi My Name is ".$name;
}
}
$name = "Deval Patel";
$iu = new Iuser($name);
$wu = new Wuser($name);
$user = new Name($wu);
echo $user->getName();
Solution Using Interface
interface User{
public function getName();
}
class Iuser implements User{
private $name;
function __construct($name){
$this->name = $name;
}
function getName(){
return strtoupper($this->name);
}
}
class Wuser implements User{
private $name;
function __construct($name){
$this->name = $name;
}
function getName(){
return strtolower($this->name);
}
}
class Name{
public $u;
function __construct(User $u){
$this->u = $u;
}
function getName(){
$name = $this->u->getName();
return "Hi My Name is ".$name;
}
}
$name = "Deval Patel";
$iu = new Iuser($name);
$wu = new Wuser($name);
$user = new Name($iu);
echo $user->getName();
Using the interface solution the getName() method is defined but not implemented in User interface (not shown in your code), so each class implementing it has to define the behaviour to get the name, whilst using abstract classes you may define the standard way to get the name, and override or overload the method in child classes when necessary.
So for your code, I think the best solution is the abstraction.
Remember to use interfaces when you want to force the developer to code the getName() method, and for abstract classes just to allow the developer to use the parent method getName(), or override/overload if necessary. Interfaces gives you more control and code reutilization.
Review the PHP object interfaces doc and PHP class abstraction doc, it may shed some light on your doubt.
I would use classes because you can reuse some code.
abstract class User
{
protected
$name;
public function __construct($name)
{
$this->name = $name;
}
abstract function getName();
}
class Iuser extends User
{
public function getName()
{
return strtoupper($this->name);
}
}
class Wuser extends User
{
function getName(){
return strtolower($this->name);
}
}
This way you can still use polymorphism and reuse some common code.
It's better then interface because the code reuse. You can still use an interface for the abstract class User but I think it would be an overkill.
I'm sorry for what is probably an easy question, but I'm having trouble finding anything about it on the web. I have two classes, for the purposes of this post let's call them user and address, looking like this:
class user {
public $firstName;
public $lastName;
public $address;
}
class address{
public $streetAddress;
public $streetName;
public $city;
public $state;
public $zip;
}
In this case, I want the $address parameter in the user class to be of type "address" defined by the second class. Can I do this in PHP? My end goal is to be able to consume properties like this:
$userZip = $user->address->zip;
Am I on the right track or way off?
Try like this;
<?php
class user {
public $firstName='bulent';
public $lastName ='kocaman';
public $address;
public function __construct()
{
$this->address = new address();
}
}
class address{
public $streetAddress=1;
public $streetName=2;
public $city=3;
public $state=4;
public $zip=5;
}
$user = new user();
print_r($user->address->zip);
?>
or
<?php
class user extends address{
public $firstName='bulent';
public $lastName ='kocaman';
}
class address{
public $streetAddress=1;
public $streetName=2;
public $city=3;
public $state=4;
public $zip=5;
}
$user = new user();
print_r($user->zip);
?>
$address = new $address();
I believe this is what you're looking for.
In PHP variable types are defined by the data they are assigned to. If you want $address in class 1 to be of type $address (class 2) then all you have to do is assign it to an instance of $address and it will take on that type. You will then be able to access any of the $address properties by using:
$user->address->zip
In short, yes you are on the right track. Look into adding constructors to your classes.
i'm having a trouble at the moment that i'm trying to access from a method of Wolf class to other method of Dog class. That's the code:
index.php
require_once 'Wolf.php';
require_once 'Dog.php';
class StackOverflowExample{
public static function run(){
$dog = new Dog("Cokey");
$wolf = new Wolf("Wolfenstein");
$wolf->hunt();
}
}
StackOverflowExample::run();
Wolf.php
class Wolf {
private $_name;
public function __construct($name){
$this->_name = $name;
}
public function hunt(){
return $dog->setLife(0);
}
}
Dog.php
class Dog {
private $_name;
private $_life= 100;
public function __construct($name){
$this->_name = $name;
}
public function setLife($life){
$this->_life = $life;
}
public function getLife(){
return $this->_life;
}
}
It gives me the next errors:
· Undefined variable: dog
· Call to a member function setLife() on a non-object
I have spent two days searching and I still having no way to fix.
Thanks you and sorry for my bad english.
What you should do is to pass $dog around, in the hunt method, like this:
public function hunt(Dog $dog){
return $dog->setLife(0);
}
You get the ability to validate that the dog variable is an instance of the expected class.
This method of passing objects around is called dependency injection.
If you want to pass around all sorts of animals you need to use another concept called inheritance:
abstract class Animal
{
public $_life;
public function setLife($life){
$this->_life = $life;
}
public function getLife(){
return $this->_life;
}
}
class Dog extends Animal
{
private $_name;
public function __construct($name){
$this->_name = $name;
}
}
class Wolf {
private $_name;
public function __construct($name){
$this->_name = $name;
}
public function hunt(Animal $animal){
return $animal->setLife(0);
}
}
Notice that the common methods and variables live in the parent class, and that you can create as many types as animals as you want.
If you follow this example you should find a way to declare the animal's name variable in the Animal class so you don't have to redefine it in all subclasses ;)
Replace
$dog->setLife(0);
by
Dog::setLife(0);
I've been programming mainly with JAVA and have written procedural programs in PHP, but now I'm try to write some OOP bases programs in PHP and I'm facing a problem.
I've got two files , Zoo.php and Dog.php , each contains a class.
Dog.php:
<?php
class Dog {
private $name;
private $color;
public function __construct($name,$color) {
$this->name = $name;
$this->color = $color;
}
public function getName(){
return $this->name;
}
public function getColor(){
return $this->color;
}
}
And Zoo.php:
<?php
class Zoo {
private $name;
private $dogs;
public function __construct($name) {
$this->name = $name;
$dogs = array();
}
public function addDog($dogName,$dogColor){
$dog = new Dog($dogName,$dogColor);
array_push($this->dogs,$dog);
}
public function getAllDogs(){
var_dump($dogs);
}
}
echo "start";
$z = new Zoo("test_zoo");
$z->addDog("blackie","black");
$z->getAllDogs();
The code above outputs :
Fatal error: Class 'Dog' not found in C:\wamp\www\Zoo.php on line 13
I'd like to know what's wrong with the code above and how creating an object instance within another object should be done in PHP. Thanks in advance.
I gues you are not including Dog class.
<?php
include "Dog.php";
class Zoo {
/* ... */
}
Or you can use autoloading to auto include any class by default.
It's not about OOP. Just you forgot to include Dog file to use it you Zoo class :
<?php
include 'Dog.php'; // or whatever path
class Zoo { ...
Everything else should be ok and seems to be a good use of PHP OOP btw. :)
You haven't included your Dog.php in your Zoo.php. Therefore you can't create an new instance.
Just above class Zoo add this:
include("Dog.php");
class Zoo {
All you need to return function .
Three mistakes here, u'll see all of them in my code . Constructer nothing return , behave like void , so we need a method that returns dog specification.If you wanna use the classes in separate file, so u've to use php method that "include" or "require" or "require_once" etc ( see the difference of this method)
<?php
class Dog {
private $name;
private $color;
public function __construct($name,$color) {
$this->name = $name;
$this->color = $color;
}
public function getName(){
return $this->name;
}
public function getColor(){
return $this->color;
}
public function getDogs(){
return array("name"=>$this->name,"color"=>$this->color);
}
}
class Zoo extends Dog{
private $name;
private $dogs=array();
public function __construct($name) {
$this->name = $name;
$dogs = array();
}
public function addDog($dogName,$dogColor){
$dog = new Dog($dogName,$dogColor);
array_push($this->dogs,$dog->getDogs());
}
public function getAllDogs(){
var_dump($this->dogs);
}
}
echo "start";
$z = new Zoo("test_zoo");
$z->addDog("blackie","black");
$z->getAllDogs();
?>
This is probably a basic question but im following this tutorial and at one point the code looks something like this.
<?php
class person
{
public $name;
public $height;
protected $social_security_no;
private $pin_number = 3242;
public function __construct($person_name)
{
$this->name = $person_name;
}
public function set_name($new_name)
{
$this->name = $new_name;
}
protected function get_name()
{
return $this->name;
}
public function get_pin_number_public()
{
$this->pub_pin = $this->get_pin_number();
return $this->pub_pin;
}
private function get_pin_number()
{
return $this->pin_number;
}
}
class employee extends person
{
public function __construct($person_name)
{
$this->name = $person_name;
}
protected function get_name()
{
return $this->name;
}
}
However when i use this
<?php include "class_lib.php";?>
</head>
<body id="theBody">
<div>
<?php
$maria = new person("Default");
$dave = new employee("David Knowler");
echo $dave->get_name();
?>
i get this error
Fatal error: Call to protected method employee::get_name() from
context '' in C:\Users\danny\Documents\Workspace\test\index.php on
line 13
The problem seems to be when i add protected to the get_name() function in the employee class but it seems to me that this is the preferred way to override in the tutorial. Any ideas?
The problem isn't that you cannot override the protected method, it's that you are calling a protected method from outside of the class.
After the class is instantiated, you can call a public method which in turn could call get_name() and you will see that the code will work as expected.
For example:
class employee extends person {
function __construct($person_name){
$this->name = $person_name;
}
protected function get_name() {
return $this->name;
}
public function name()
{
return $this->get_name();
}
}
$dave = new employee("David Knowler");
echo $dave->name();
In your example, you would probably be best making get_name() public.
"The problem seems to be when i add protected to the get_name() function in the employee class" -- this is your answer. A protected method can only be called from the very same class or subclasses, not "from the outside". Your method has to be public if you want to use it this way.
You can access get_name() within person class or employee class not outside of these two classes.
check protected visibility
http://php.net/manual/en/language.oop5.visibility.php