Jump to content

Extending Classes


Suchy

Recommended Posts

Im having problem extending a class.

 

<?php
// Parent class
class Profile
{
    protected $id, $result, $info;    

    protected function get_id()
    { 
        return $_GET['id'];
     }

     protected get_info()
     {
         $id = $this->get_id();
         $result = mysql_query("SELECT * FROM profiles WHERE id = '$id' ");
         return  mysql_fetch_array($result);
      }

     public get_fname()
     {
         $info = $this->get_info();
         return $info['fname'];
     }  

     public get_lname()
     {
         $info = $this->get_info();
         return $info['lname'];
     }  
}


// Child class
class Profile_new extends Profile
{
     protected get_info()
     {
         $id = $this->get_id();
         $result = mysql_query("SELECT * FROM new_profiles WHERE id = '$id' ");
         return  mysql_fetch_array($result);
      }
}

// Webpage
$profile = new Profile_new;
$first_name = $profile->get_fname();
$last_name = $profile->get_lname();
?>

 

For some reason I still getting first + last names of people from the profiles table, and not from the new_profiles table.

 

How can I fix this ?

Link to comment
https://forums.phpfreaks.com/topic/246377-extending-classes/
Share on other sites

Well, you marked this topic as solved, but the reason is you that when accessing a base classes methods, once inside those methods you only have access to other base class attributes/methods. So even though you overrided the base classes method, while inside the other method of the base class, it doesn't know of your override more of less. I can discuss this in more detail if you would like.

Link to comment
https://forums.phpfreaks.com/topic/246377-extending-classes/#findComment-1265199
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.