accessing class members from an extending class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

accessing class members from an extending class

Post by s.dot »

Code: Select all

<?php

class test
{
	protected $boolean = true;
	
	public function setBoolean($bool)
	{
		$this->boolean = (bool) $bool;
	}
	
	public function getBoolean()
	{
		//show this class boolean
		var_dump($this->boolean);
		
		//show class foo boolean
		$foo = new foo();
		$foo->getTestBoolean();
	}
}

class foo extends test
{
	protected function getTestBoolean()
	{
		var_dump($this->boolean);
	}
}

$test = new test();
$test->setBoolean(false);
$test->getBoolean();
This outputs:

Code: Select all

bool(false)
bool(true)
How can I access $this->_boolean from within class foo (and maintain the set value)?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

What's not working? The first false is from $test which you set to false. The second true is from $foo which still has the default value of true.
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

So how do I make the class $foo recognize that $test has had it's member set to false?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Well ... they are two different object, so:

Code: Select all

public function getBoolean()
   {
      //show this class boolean
      var_dump($this->boolean);
      
      //show class foo boolean
      $foo = new foo();
      $foo->setBoolean($this->boolean);
      $foo->getTestBoolean();
   }
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

This smells of bad design to me. Do you agree?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

My real-world usage would be this:

Code: Select all

public function getStats($input, $recurse=false)
{
	if (is_file($input))
	{
		$ret = new phpsloc_file($input);
	} elseif (is_dir($input) && !$recurse)
	{
		$ret = new phpsloc_directory($input);
	} elseif (is_dir($input) && $recurse)
	{
		$ret = new phpsloc_directoryTree($input);
	} else
	{
		trigger_error('PHPsloc: Could not evaluate input file or directory', E_USER_ERROR);
	}
	
	$ret->_returnHTML = $this->_returnHTML;
	$ret->_PHPOnly = $this->_PHPOnly;
	$ret->_allowPHPTags = $this->_allowPHPTags;
	$ret->_allowSingleSpace = $this->_allowSingleSpace;
	$ret->_lineLength = $this->_lineLength;
	$ret->_lineCountPrecision = $this->_lineCountPrecision;
	
	return $ret->_getStat();
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The base knows about the extended classes? Yes, it smells ... ;)
Post Reply