Skip to content
WJunction - Webmaster Forum

What is the point of the php extends in relation to classes?

Status
Not open for further replies.
I just don't get it. Why create one class, eg
PHP:
class user{
function login(){
//login stuff
}
}

then use the extends keyword like so
PHP:
class admin extends user{
function admin(){
//admin stuff
}
}

Why not just put it in one big class and only call the admin function for when they are needed?
 

11 comments

An Example:

PHP:
class Curl
{
    var $url,$postparams;
    protected $curl;

    private function __construct()
    {
         $this->curl = curl_init();
    }
     
    public function submit()
    {
        //Code to submit
    }

    public function request()
    {
        //Code to request
    }
}

then you would use that as your base and create many smaller classes to use that code

PHP:
class SubmitPost extends Curl
{
     public function __construct($url,$data)
     {
          $this->url = $url;
          $this->postparams = $data;
     }

     public function go()
     {
         return $this->submit();
     }
}

aswell as:

PHP:
class RequestPage extends Curl
{
     public function __construct($url)
     {
          $this->url = $url;
     }

     public function go()
     {
         return $this->request();
     }
}

Then you have organized code for particular task and not one massive class that's jumbled up with many different things.
 
Most commonly used for shared elements of code between multiple classes. e.g a Base class might have a method for url encoding and the DDL class might have a method retuning a download url (urlencoded)
 
Speaking from the OOP perspective, inheritance is a very essential feature. Let me generalize this.

Say you have a class called flowers. Now you create another class called Rose. now the Rose class has ALL the properties of flowers so it "inherits" it from the parent aka the flowers class and the rose class is unique in itself.. Hope that makes it clear.. this was the way I was taught in grade 10 while learning Java ;)
 
Speaking from the OOP perspective, inheritance is a very essential feature. Let me generalize this.

Say you have a class called flowers. Now you create another class called Rose. now the Rose class has ALL the properties of flowers so it "inherits" it from the parent aka the flowers class and the rose class is unique in itself.. Hope that makes it clear.. this was the way I was taught in grade 10 while learning Java ;)

Well explained, I like the way you explain gunda (y)
 
Status
Not open for further replies.

About the author

T
Active Member · Joined
Picked up photoshop about half a year ago. Now i do graphics design :) Graphics/Web Design
885
Messages
0
Reactions
16
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom