Welcome Guest, Not a member yet? Register   Sign In
Extending Class, The right way? (BASIC)
#1

[eluser]solid9[/eluser]
How to extend class the right way?
And how to call a method in the parent class inside the child class?

<b>Show me sample, using the format below,</b>
parent: person.php (parent class)
child: employee.php (child class)


Thanks in advanced.
#2

[eluser]Andy78[/eluser]
I'm sure you just call the method in the child class exactly as you would in the parent class.
#3

[eluser]solid9[/eluser]
@andy78

can you show me sample.

How about I have two classes in two separate files?

Thanks in advanced.

#4

[eluser]Andy78[/eluser]
Any php oops tutorial for beginners will help you with this. To call a method in the parent class inside the child class you would simply use $this->some-method(); exactly as you would in the parent class since all methods in the parent class are also methods of the child class. Get it?

Code:
&lt;?php
class parent
{
      private $firstname = "Coders";
      protected $lastname = 23;

      protected function getData()
      {
             return $this->firstname;
      }
}

class children extends parent
{
      function __construct()
      {
         echo $this->getData();
      }
}

$a = new children();
?&gt;
#5

[eluser]boltsabre[/eluser]
And if you're child class has the same method name as the parent, it will override the parent method (by default, I'm pretty sure that there is a way to get around this but it escapes me currently). Think it's parentClassName::methodName

This is a good intro tutorial
http://www.killerphp.com/tutorials/objec.../index.php

#6

[eluser]soprabaixar1[/eluser]
See: http://ellislab.com/forums/viewthread/204977/




Theme © iAndrew 2016 - Forum software by © MyBB