Page 1 of 1
parent::setup->my_vars
Posted: Tue Mar 06, 2007 1:05 am
by nwp
Can I do Someting like this ??
or
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 ??
Posted: Tue Mar 06, 2007 1:14 am
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();
?>
Posted: Tue Mar 06, 2007 2:44 am
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
Posted: Tue Mar 06, 2007 2:52 am
by volka
nwp wrote:private $setup;
Posted: Tue Mar 06, 2007 2:58 am
by nwp
So if I use protected instead of private would it work ??
EDIT
------
Can a child class use only public vars and functions ??
Posted: Tue Mar 06, 2007 3:10 am
by volka
You wouldn't have asked without testing or reading
http://de2.php.net/manual/en/language.o ... bility.php , would you?

Posted: Tue Mar 06, 2007 3:13 am
by nwp
protected are also invisible <-- Written there and Its Undefined
Posted: Tue Mar 06, 2007 3:17 am
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
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
Posted: Tue Mar 06, 2007 3:23 am
by nwp
OK Thanks