Jump to content

understanding php inheritance


Liquid Fire

Recommended Posts

Ok, i am trying to start a framework and a big part of it is going to be the base_data class which will have common functions for objects the is retrived from or saved to a database. here is code to help explain my question:

 

base_data class:

<?php
class base_data
{
    private function set_data($data, $value)
    {
        $this->$data = $value;
    }
}
?>

 

application class extends base_data

<?php
    public function __call($function, $parameters)
    {
        list($function, $data) = explode("_", $function, 2);
        
        switch($function)
        {
            case "set":
                $value = $parameters[0];
                parent::set_data($data, $value);
                return true;
                break;
                
            case "get":
                return $this->$data;
                break;
                
            default:
                return false;
                break;
        }
    }

    private $id;
?>

 

Now i am using parent::set_data() because i have always seen it used that way(where is C++ i would normally do this->set_data or php version $this->set_data).  Now with this code would parent::set_data know about $id in application, my guess is no?  if not can i call set_data like $this->set_data and then will it know about $id?  If both are false the i would have to pass a reference of the object to complete this functionality which seems very un-OOP like.

Link to comment
Share on other sites

Calling parent::set_data() will give you access to variables in the extended class, but not with the scope that you have them at.

 

Variables and methods marked as 'private' are ONLY accessible through the class they reside in. If you want other classes that extend your class to access them, you need to mark them as 'protected'. Marking them as 'public' will obviously let anything use those methods/variables, regardless of whether or not they extend the class or not.

 

At minimum to make your example work, you would need to mark set_data() as protected in the base_data class, and $id as protected in the application class.

 

Here's a simplified example of how you can get it to work.

 

<?php

class base
{
protected function set_data($data)
{
	echo $this->id;
}
}

class application extends base
{
protected $id = 14;

public function set($value)
{
	parent::set_data($value);
}
}


$class = new application();
$class->set('test');

?>

Link to comment
Share on other sites

what if i keeped the private and did this

<?php
    public function __call($function, $parameters)
    {
        list($function, $data) = explode("_", $function, 2);
        
        switch($function)
        {
            case "set":
                $value = $parameters[0];
                $this->set_data($data, $value);
                return true;
                break;
                
            case "get":
                return $this->$data;
                break;
                
            default:
                return false;
                break;
        }
    }

    private $id;
?>

Link to comment
Share on other sites

No, that won't work. Something marked as private can only be accessed within the class it is defined in. No other class can access it, even if it extends it.

 

Marking them as protected is necessary to allow classes that extend your base class access to variables and methods in other classes.

 

I would suggest reading up on object member visibility if you are still a bit unclear as to the difference between private, protected, and public. It's in the php manual here: http://www.php.net/manual/en/language.oop5.visibility.php

Link to comment
Share on other sites

Your right, kinda of a brain fart had. i do need to use protected.

 

My question is there a difference from

 

parent::set_data()

and

$this->set_data()

 

From what i know about OOP(which i think is pretty good) the parent::set_data would only have access to the members inside the parent class but $this->set_data i think woudl have access to the child class since i am calling it from the child class.

Link to comment
Share on other sites

Either way will have access to everything listed as protected or public in either class. It doesn't matter which way you call it.

 

The only real difference between calling $this->set_data() and parent::set_data() is that if you defined a method called set_data() in the extended application class, that would get called instead of the set_data() in the base class. Using parent::set_data() ensures that the set_data() method in the base class is always called, regardless of whether or not you created a function of the same name in the extended class.

 

 

Try this example to see what I mean.

 

<?php

class base
{
protected function set_data()
{
	echo "I'm in the base class<br />";
}
}

class application extends base
{
public function set()
{
	// call to the parent class
	parent::set_data();

	// call to the current class
	$this->set_data();
}

protected function set_data()
{
	echo "I'm in the application class<br />";
}
}


$class = new application();
$class->set();

?>

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.