Jump to content

Help in OOP


web4

Recommended Posts

 
/*
/*
"Private" methods and properties are only visible within the class that defines them,
      /* "Protected" methods and properties are visible to both their defining class and any  /*            child(inherited) classes.
      /*  Attempts to access these properties or methods outside their visible area typically
      /*  produces a fatal error that stops script execution.
      /*
      /*
      /*                                                   */

      <?php
      class Mammal {

      public $name;
      protected $age;
      private $species;
      }

     class Human extends Mammal {

      }

      $mammal = new Mammal;
      echo $mammal->name = 'William'; //ok
      echo $mammal->age = 3;  //fatal error
      echo $mammal->species = 'Whale'; //fatal error

      $human = new Human;
       echo $human->name = 'Barry';  //ok
       echo $human->age = 1;   //fatal error
      echo $human->species = 'Boy';   //undefined

      ?>

 

I am confuse on that example about OOP can anyone explain to me why the

$mammal->species = 'Whale'; 

gets fatal error and

$human->species = 'Boy';

  gets undefined only? because they are the same private and if you get the definition of private from the comments above private is only "visible on it's class only" and so in the code

$human->species = 'Boy';

should get also fatal error because the species variable is private to mammal class and even though it is inherited by the human class it should get fatal error because species is private in mammal class..

 

I hope i delivered my question clearly thanks.

 

Link to comment
Share on other sites

It throws an undefined error with the human becaue private members ARE NOT passed to child classes.  The species field doesn't exist in human.  It is, by definition, undefined.

 

The fatal errors are a result of attempting to access something that DOES exist, but which you don't have permission to interact with.

Link to comment
Share on other sites

Thanks for your reply but in the human class with the declaration

$human->species = 'Boy';

it tries to access the species private variable and thus must have an fatal error...

cause from my learning OOP i thought that if you have a parent class and a child class

you can inherit the properties of the parent class(ex..all the variables are public)

and my conclusion to that is you can

access variables in parent class using the child class )

Link to comment
Share on other sites

it tries to access the species private variable and thus must have an fatal error...

 

No. The variable $species does NOT exist within Human to show you try:

 

$mammal->age = 1;//Fatal Error
$mammal->species = 'Whale';//Fatal Error
$human->age = 1;//Fatal Error
$human->species = 'Boy';//Undefined

 

Only public and protected fields are passed by inheritance private are NOT. Also you can only access variables directly if you declare them public (in which case they are called properties):

 

$human->name = 'George';

 

However you should avoid this and use setters (mutators) and getters (accessors) instead. And declare your class members private (only in rare cases should you use protected)

 

class Mammal {
     private $name;
     private $age;
     private $species;
     
     public function setName($name) {
         $this->name = $name;
     }
     
     public function setAge($age) {
         $this->age = $age;
     }
     
     public function setSpecies($species) {
         $this->species = $species;
     }
}

class Human extends Mammal {
}

$human = new Human();
$human->setName('George');
$human->setAge(13);
$human->setSpecies('Boy');
var_dump($human);

 

Works with no errors. To understand why this works and the other didn't is because of scope resolution the below example also works:

 

class Person {
    private $name;
    
    public function equals(Person $p) {
         return $p->name === $this->name;
    }
}

Link to comment
Share on other sites

thanks for your reply only one questions remains in mind...

echo $mammal->species = 'Whale'; //fatal error

 

i wonder why the error is fatal because the definition of "private" is

 

methods and properties are only visible within the class that defines them

 

however that declaration (in my understanding and i know it is wrong and it needs to be clear up)

 

it shouldn't have a fatal error because

$mammal->species 

that code

 

$mammal is a class right? then we access the private variable with the "same class"

 

through $mammal->species and it shouldn't have an error because this doesn't violate

 

the private definition because i use $mammal->species to access the private variable

 

which means the same class...i hope i deliver it clearly my question...

Link to comment
Share on other sites

thanks for your reply only one questions remains in mind...

echo $mammal->species = 'Whale'; //fatal error

 

i wonder why the error is fatal because the definition of "private" is

 

methods and properties are only visible within the class that defines them

 

however that declaration (in my understanding and i know it is wrong and it needs to be clear up)

 

it shouldn't have a fatal error because

$mammal->species 

that code

 

$mammal is a class right? then we access the private variable with the "same class"

 

through $mammal->species and it shouldn't have an error because this doesn't violate

 

the private definition because i use $mammal->species to access the private variable

 

which means the same class...i hope i deliver it clearly my question...

 

$mammal is an object, an instance of the class. Think of it like making your own variables, which can have properties and functions which act on the variables within the object, these variables and functions in OOP are called properties and methods, respectively.

 

When people say that private properties can only be accessed by the class in which they are declared, they mean exactly that, if you want to be able to update the value stored in the variable, you must use a public setter, and in order to retrieve it, a public getter.

 


class mammal
{

   private $_name;
   private $_age;
   private $_species;

   public function setName($name)
   {
      $this->_name = $name;
   }

   public function setAge($age)
   {
      $this->_age = (int)$age;
   }

   public function setSpecies($species)
   {
      $this->_species = $species;
   }

   public function getName()
   {
      return $this->_name;
   }

   public function getAge()
   {
      return $this->_age;
   }

   public function getSpecies()
   {
      return $this->_species;
   }

   // magic method
   public function __toString()
   {
      return $this->_name . ' the ' . $this->_species . ' is ' . $this->_age . ' years old.';
   }

}

$mammal = new mammal();

$mammal->setName('Dillan');
$mammal->setAge('9');
$mammal->setSpecies('Dog');

echo $mammal->getName() . "<br >\r\n";
echo $mammal->getAge() . "<br >\r\n";
echo $mammal->getSpecies() . "<br ><br >\r\n";

echo $mammal;

 

PHP.net on OOP

http://uk.php.net/oop

http://uk3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring

http://uk3.php.net/__tostring

 

PHPFreaks Tutorial on OOP

http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect

http://www.phpfreaks.com/tutorial/oo-php-part-2-boring-oo-principles

http://www.phpfreaks.com/tutorial/oo-php-part-3-uml-classes-and-relations

 

 

Hope that helps a little.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.