Jump to content

Recommended Posts

Hello,

 

Objects which are static in classes can be accessed with parent::$obj and thus $this cannot be used within the object.

 

So what is the non-static equivilent I can use to access the classes parent in a non-static way? When I try to use the static method (i.e. parent::$obj) it errors out: "Fatal error: Access to undeclared static property: class::$obj in [...]".

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/
Share on other sites

Okay, here is a code example:

 

class base
{

var $var;

function __construct ( $var )
{
	$this->var = $var;
}

function func (  )
{
	print $this->var;
}

}

class stend extends base
{

var $var; //Different var specific to the stend class.

function need_var (  )
{
	return --THIS IS THE VALE--
}

}

$class = new base ( "toenail" );
$stended = new stend;

print $stended->need_var();

I know that for that example you could change it and remove the need for the other classes, but in my case I need the classes, so I have the problem presented there.

 

So where it says "--THIS IS THE VALE--" how can I reference the $var var from the base class?

You can't...$class and $stend are completely independent objects. I think you are looking for something more like this:

 

<?php
class base
{

var $var;

function __construct ( $var )
{
	$this->var = $var;
}

function func (  )
{
	print $this->var;
}

}

class stend
{

       var $child;

function __construct ( $var_obj )
{
           $this->child = $var_obj;
        }
function need_var (  )
{
	return $this->child->var;
}

}

$base= new base ( "toenail" );
$stended = new stend($base);
print $stended->need_var();
?>

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.