Jump to content

[SOLVED] Parent variables


coldkill

Recommended Posts

Hello all.

 

I'm building a new website for myself (well it's a CMS so I can basically re-use it for other projects as well). It uses classes and extended classes but I have one problem. I can't access variables from the parent class. It seems that the parent:: scope resolution operator with variables only works with PHP5 and later. My server currently runs PHP4 so it doesn't work.

 

Anyone know how to access variables from a parent class in PHP4? I tried using functions as well and that didn't work. IE:

<?php 
class b extends a
{
     function b()
     {
          echo parent::aFunction();
     }
}
?>

 

Thanks in advance.

Cold

Link to comment
https://forums.phpfreaks.com/topic/63423-solved-parent-variables/
Share on other sites

hmmm as far as i'm aware (or can remember from using php 4), parent works fine in php 4.

 

parent::method() = used to call a function that exists in parent class but has been overwritten by descendent

$this->method() = call a method that has been defined in either descendent class or parent

$this->variable = used to access a variable that has either been set in parent class or overwritten by descendent

 

ref: http://uk.php.net/keyword.parent - under PHP4 OOP manual

 

you mention variables, but your example would imply a method call - which one is it you're trying to do, and what result are you getting?

The function works fine. I'm trying to get a variable from a parent function (one defined with the var constructor).

 

The only way I can think of how to do this is use a function and return the value of the variable, but that didn't work when I tested it...

 

Thanks,

Cold

class Class1 {
	var $a = 'foo';

	function Class1() {
		return $this->a;
	}

}

class Class2 extends Class1 {

	function Class2() {
		echo $this->a;
		echo $this->Class1();
	}

}

$c = new Class2();
//returns 'foofoo';

 

Does that not work?  I know it works in PHP 5...

 

Or is there a reason you're specifically trying to use the parent:: syntax?

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.