Jump to content

[SOLVED] Non-Static Parent:: Equivilent


Millar

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();
?>

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.