Jump to content

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

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.