parent::setup->my_vars

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
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

parent::setup->my_vars

Post by nwp »

Can I do Someting like this ??

Code: Select all

parent::setup->my_vars
or

Code: Select all

parent::setup::my_vars
From a Child class that extends the parent class.
Where setup is a object in the parent class.

I've tested it . but when i was typing it in Zebd Studio it was Showing me Syntax Error On parent::setup->my_vars
and
parent::setup::my_vars
So How can i fix it ??
or can do something like this ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
class foo {
  var $my_vars;
  function foo() {
  	$this->my_vars = date('H:i:s');
  }
}

class bar {
	var $setup;
	
	function bar() {
		$this->setup = new foo;
	}
}

class barEx extends bar {
	
	function xyz() {
		echo $this->setup->my_vars;
	}
}

$b = new barEx;
$b->xyz();
?>
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

But this doesn't work
~~~~~~~~~~~~~

Code: Select all

<?php
error_reporting(E_ALL);
class foo
	{
    private $my_vars;
    public function __construct()
  	  {
    	  $this->my_vars = date('H:i:s');
  	  }
	}
class bar
	{
  	private $setup;
    public function __construct()
    	{
      	$this->setup = new foo();
      }
	}
class barEx extends bar
	{
  	public function xyz()
    	{
      	echo $this->setup->my_vars;
      }
	}
$b = new barEx;
$b->xyz();
?>
Brpwser wrote:Notice: Undefined property: barEx::$setup in D:\http\xampp\htdocs\tmp_rapid_php\prev4~.php on line 23

Notice: Trying to get property of non-object in D:\http\xampp\htdocs\tmp_rapid_php\prev4~.php on line 23
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

nwp wrote:private $setup;
http://de2.php.net/manual/en/language.oop5.visibility.php wrote:Private limits visibility only to the class that defines the item.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

So if I use protected instead of private would it work ??
EDIT
------
Can a child class use only public vars and functions ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You wouldn't have asked without testing or reading http://de2.php.net/manual/en/language.o ... bility.php , would you? ;)
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

protected are also invisible <-- Written there and Its Undefined
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Ok, I take you replaced both occurences of privat by protected. Now you only get one warning, right?

Code: Select all

<?php
error_reporting(E_ALL);
class foo
{
  protected $my_vars;
  public function __construct()
  {
    $this->my_vars = date('H:i:s');
  }
}

class bar
{
  protected $setup;
  public function __construct()
  {
    $this->setup = new foo();
  }
}

class barEx extends bar
{
  public function xyz()
  {
    echo $this->setup->my_vars;
  }
}

$b = new barEx;
$b->xyz();
?>
barEx::xyz() tries to access the protected member setup declared in class bar. barEx extends bar therefore
http://de2.php.net/manual/en/language.oop5.visibility.php wrote:Protected limits access to inherited and parent classes (and to the class that defines the item)
applies. But barEx does not extend foo, in fact it's not related at all. Therefore it cannot access the protected member of foo neither can bar.
=> Fatal error: Cannot access protected property foo::$my_vars
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

OK Thanks
Post Reply