Jump to content

[SOLVED] Extending classes


creomire

Recommended Posts

Hi, I am extending classes but having a slight problem. I have a variable in the parent class that is set by a parent object, but the child class sees the variable as null.

 

Here's a simplified version of the code:

 

class parent
{
    var $config = "";

    function initiate()
    {
        $this->config = "hello world";
    }
}

class child extends parent
{
    function display()
    {
        $content = $this->config;
        return $content;
    }
}

$parent = new parent;
$child = new child;

$parent->initiate();
$content = $child->display();
echo $content;

 

Any help appreciated.

Link to comment
Share on other sites

I've never used a class in PHP, only C++.  However, from looking at the code, it looks like parent and child are separate objects.  Don't you have to call initiate() for the child object?  Just because the names are $parent and $child does not mean anything.  It is the object that matters, and they are separate instances.

Link to comment
Share on other sites

You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do.  In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance.

 

That probably sounds confusing, perhaps someone else can explain it more eloquently.

 

But try this...

 

$child = new child;
$child->initiate();
$child->display();

Link to comment
Share on other sites

try

class parent
{
    var $config = "";

    function initiate()
    {
        $this->config = "hello world";
    }
}

class child extends parent
{
    function display()
    {
        $content = $this->config;
        return $content;
    }
}

//$parent = new parent; //removed
$child = new child;

//$parent->initiate(); //removed
$child->initiate();
$content = $child->display();
echo $content;

 

 

EDIT also this should be in the OOP section!

Link to comment
Share on other sites

try

class parent
{
    var $config = "";

    function initiate()
    {
        $this->config = "hello world";
    }
}

class child extends parent
{
    function display()
    {
        $content = $this->config;
        return $content;
    }
}

//$parent = new parent; //removed
$child = new child;

//$parent->initiate(); //removed
$child->initiate();
$content = $child->display();
echo $content;

 

 

EDIT also this should be in the OOP section!

 

Surely that is wrong. A class cannot extend another unless they have both been declared. In your example, the parent class has not been declared, and therefore child cannot be an extension of it.

Link to comment
Share on other sites

You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do.  In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance.

 

That probably sounds confusing, perhaps someone else can explain it more eloquently.

 

But try this...

 

$child = new child;
$child->initiate();
$child->display();

 

I'll test this.

Link to comment
Share on other sites

You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do.  In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance.

 

That probably sounds confusing, perhaps someone else can explain it more eloquently.

 

But try this...

 

$child = new child;
$child->initiate();
$child->display();

 

I got a workaround with this, many thanks.

Link to comment
Share on other sites

It doesn't work like that, you need to declare the parent class first.

 

Tried and tested code

Note: the parent to myparent etc

 

<?php

class myparent
{
    var $config = "";

    function initiate()
    {
        $this->config = "hello world";
    }
}

class mychild extends myparent
{
    function display()
    {
        $content = $this->config;
        return $content;
    }
}

//$parent = new parent; //removed
$child = new mychild;
//$parent->initiate(); //removed
$child->initiate();
$content = $child->display();
echo $content;

?>

Link to comment
Share on other sites

It doesn't work like that, you need to declare the parent class first.

 

Tried and tested code

Note: the parent to myparent etc

 

<?php

class myparent
{
    var $config = "";

    function initiate()
    {
        $this->config = "hello world";
    }
}

class mychild extends myparent
{
    function display()
    {
        $content = $this->config;
        return $content;
    }
}

//$parent = new parent; //removed
$child = new mychild;
//$parent->initiate(); //removed
$child->initiate();
$content = $child->display();
echo $content;

?>

 

Whoops! My bad. I sincerely apologise... that must have been annoying there. The parent class doesn't have to have an instance made, just declared in the php script before the child.

 

Tell me, if I did create an instance of $parent, and also an instance of $child... and then only used the $child operator (seeing as it does everything anyway), would this use more memory than calling $child alone? Is there an advantage at all?

 

Many thanks for the reply.

Link to comment
Share on other sites

I had basically the same question (see link)

and user:448191's reply was very cool but simply put

 

if $parent took up (100kb) and $child took (excluding the extension 50kb) 150kb,

then if you declare them both it will use up 250kb

 

BUT if you needed 2 instance but only the core ($parent) for 1 instance then you could have 1 parent and 1 child..

 

i hope that makes sense!

 

OK heads hurting now!

Link to comment
Share on other sites

I had basically the same question (see link)

and user:448191's reply was very cool but simply put

 

if $parent took up (100kb) and $child took (excluding the extension 50kb) 150kb,

then if you declare them both it will use up 250kb

 

BUT if you needed 2 instance but only the core ($parent) for 1 instance then you could have 1 parent and 1 child..

 

i hope that makes sense!

 

OK heads hurting now!

 

Ahh!

 

So only declare one instance if possible. Makes sense.

 

Thanks a ton.

Link to comment
Share on other sites

Yep.. but if you do need to declare twice then think if you need all the extended classes, of course you can have an extension of an extension of an extension of an extension  :o etc so you can declare in the middle.. its all about planning ahead!

 

EDIT:

if this topic is closed please click Topic Solved (bottom left)

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.