Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
working with cl*** inheritance
Old
  (#1)
Jeff Taylor
Guest
 
Posts: n/a
Default working with cl*** inheritance - 05-14-2007, 03:55 AM

Hey all, got a slight problem, where for some reasons my variables dont seem
to be getting stored in the child cl***:

e.g

cl*** Parent
{
$private type;

public function __construct()
{
}

public function GetType()
{
return $this->type;
}
}

cl*** Child implements Parent
{
$public function __construct()


$this->type= 'Child';
}
}

$Child= new Child();
echo $Child->getType;

Can u see any reason why the type would return null?

Thanks,

Jeff

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: working with cl*** inheritance
Old
  (#2)
Jeff Taylor
Guest
 
Posts: n/a
Default Re: working with cl*** inheritance - 05-14-2007, 03:55 AM

OOPS!... typo

Please replace implements with extends:

cl*** Child extends Parent

Sorry about that
""Jeff Taylor"" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED...
> Hey all, got a slight problem, where for some reasons my variables dont

seem
> to be getting stored in the child cl***:
>
> e.g
>
> cl*** Parent
> {
> $private type;
>
> public function __construct()
> {
> }
>
> public function GetType()
> {
> return $this->type;
> }
> }
>
> cl*** Child implements Parent
> {
> $public function __construct()
>
>
> $this->type= 'Child';
> }
> }
>
> $Child= new Child();
> echo $Child->getType;
>
> Can u see any reason why the type would return null?
>
> Thanks,
>
> Jeff
>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: working with cl*** inheritance
Old
  (#3)
Jeff Taylor
Guest
 
Posts: n/a
Default Re: working with cl*** inheritance - 05-14-2007, 03:55 AM

I forgot to mention that I have some __construct operations in the parent
cl***, and I want to add to those (and overwrite sometimes) in the child
cl*** __construct

..... i.e Parent cl*** __construct
$this->foo = 'Bar';
$this->foo2 = '10';

Child cl*** __construct
$this->foo.='Bar';
$this->foo2 .= rand(1,6);
$this->type = 'Child';

Outputs of variables foo == Bar Bar
foo2 == 11-16
type == Child

Thanks



""Jeff Taylor"" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED...
> OOPS!... typo
>
> Please replace implements with extends:
>
> cl*** Child extends Parent
>
> Sorry about that
> ""Jeff Taylor"" <EMAIL REMOVED> wrote in message
> news:EMAIL REMOVED...
> > Hey all, got a slight problem, where for some reasons my variables dont

> seem
> > to be getting stored in the child cl***:
> >
> > e.g
> >
> > cl*** Parent
> > {
> > $private type;
> >
> > public function __construct()
> > {
> > }
> >
> > public function GetType()
> > {
> > return $this->type;
> > }
> > }
> >
> > cl*** Child implements Parent
> > {
> > $public function __construct()
> >
> >
> > $this->type= 'Child';
> > }
> > }
> >
> > $Child= new Child();
> > echo $Child->getType;
> >
> > Can u see any reason why the type would return null?
> >
> > Thanks,
> >
> > Jeff
> >

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: working with cl*** inheritance
Old
  (#4)
Roman Neuhauser
Guest
 
Posts: n/a
Default Re: [PHP] Re: working with cl*** inheritance - 05-14-2007, 03:55 AM

# EMAIL REMOVED / 2007-03-20 19:14:17 +1030:
> ""Jeff Taylor"" <EMAIL REMOVED> wrote in
> message news:EMAIL REMOVED...
> > Hey all, got a slight problem, where for some reasons my variables
> > dont seem to be getting stored in the child cl***:
> >
> > e.g
> >
> > cl*** Parent
> > {
> > $private type;
> >
> > public function __construct()
> > {
> > }
> >
> > public function GetType()
> > {
> > return $this->type;
> > }
> > }
> >
> > cl*** Child implements Parent
> > {
> > $public function __construct()
> >
> >
> > $this->type= 'Child';
> > }
> > }
> >
> > $Child= new Child();
> > echo $Child->getType;
> >
> > Can u see any reason why the type would return null?

>
> OOPS!... typo
>
> Please replace implements with extends:
>
> cl*** Child extends Parent


The code you posted doesn't parse either way, next time please post the
real thing. There's several problems in the code as posted, and either
could cause your problem. You're probably looking for this:

cl*** parent {
public function __construct() {}
private $type;
protected function settype($type) {
$this->type = $type;
}
public function gettype() {
return $this->type;
}
}
cl*** child {
public function __construct() {
$this->settype('Child');
}
}

If all instances of parent subcl***es must have a type ***igned for
their whole lifetime it's better to have parent require it, and not
provide its descendants a way to change the type during their life:

cl*** parent {
protected function __construct($type) {
$this->type = $type;
}
private $type;
public function gettype() {
return $this->type;
}
}
cl*** child {
public function __construct() {
parent::__construct('Child');
}
}


--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] working with cl*** inheritance
Old
  (#5)
Jim Lucas
Guest
 
Posts: n/a
Default Re: [PHP] working with cl*** inheritance - 05-14-2007, 03:55 AM

Jeff Taylor wrote:
> Hey all, got a slight problem, where for some reasons my variables dont seem
> to be getting stored in the child cl***:
>
> e.g
>
> cl*** Parent

At least in PHP 5.2.1 on windows xp (for testing only), the cl*** name
Parent is a reserved cl*** name, you cannot define a cl*** by that name.

> {
> $private type;
>
> public function __construct()
> {
> }
>
> public function GetType()
> {
> return $this->type;
> }
> }
>
> cl*** Child implements Parent
> {
> $public function __construct()

public should not be prefixed with '$'
you are missing your open '{' for this method
>


You need to call
parent::__construct();

>
> $this->type= 'Child';
> }
> }
>
> $Child= new Child();
> echo $Child->getType;

also, you are calling a method, not accessing a property.

it should be
echo $Child->getType();

>
> Can u see any reason why the type would return null?
>
> Thanks,
>
> Jeff
>
>

well, for starters, I think it is incorrect to have public and private
prefixed with a '$'. But then you have it on some, but not all of
them.....

Besides that, does it give you an error? What does it return if it
doesn't return 'Child'??

What version of PHP are you running on. there is a differnet way the
constuct() method gets called.

public and private are not available in versions older than php 5

in PHP version < 5 you have to have a method named the same as the cl***
that you are creating.

Try this:
<?php

cl*** myParent {
# private $type; #<--- With this, I cannot echo $type

var $type; #<--- With this, I CAN echo $type

public function __construct() {
}

public function myParent() {
$this->__construct();
}
public function getType() {
echo $this->type;
}
}

cl*** myChild extends myParent {
public function __construct() {
parent::__construct();
$this->type = 'Child';
}
public function myChild() {
$this->__construct();
}
}

$Child = new myChild();
echo $Child->getType();

?>
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] working with cl*** inheritance
Old
  (#6)
Jim Lucas
Guest
 
Posts: n/a
Default Re: [PHP] working with cl*** inheritance - 05-14-2007, 03:55 AM

Jim Lucas wrote:
> Jeff Taylor wrote:
>> Hey all, got a slight problem, where for some reasons my variables
>> dont seem
>> to be getting stored in the child cl***:
>>
>> e.g
>>
>> cl*** Parent

> At least in PHP 5.2.1 on windows xp (for testing only), the cl*** name
> Parent is a reserved cl*** name, you cannot define a cl*** by that name.
>
>> {
>> $private type;
>>
>> public function __construct()
>> {
>> }
>>
>> public function GetType()
>> {
>> return $this->type;
>> }
>> }
>>
>> cl*** Child implements Parent
>> {
>> $public function __construct()

> public should not be prefixed with '$'
> you are missing your open '{' for this method
>>

>
> You need to call
> parent::__construct();
>
>>
>> $this->type= 'Child';
>> }
>> }
>>
>> $Child= new Child();
>> echo $Child->getType;

> also, you are calling a method, not accessing a property.
>
> it should be
> echo $Child->getType();
>
>>
>> Can u see any reason why the type would return null?
>>
>> Thanks,
>>
>> Jeff
>>
>>

> well, for starters, I think it is incorrect to have public and private
> prefixed with a '$'. But then you have it on some, but not all of
> them.....
>
> Besides that, does it give you an error? What does it return if it
> doesn't return 'Child'??
>
> What version of PHP are you running on. there is a differnet way the
> constuct() method gets called.
>
> public and private are not available in versions older than php 5
>
> in PHP version < 5 you have to have a method named the same as the cl***
> that you are creating.
>
> Try this:
> <?php
>
> cl*** myParent {
> # private $type; #<--- With this, I cannot echo $type
>
> var $type; #<--- With this, I CAN echo $type
>
> public function __construct() {
> }
>
> public function myParent() {
> $this->__construct();
> }
> public function getType() {
> echo $this->type;
> }
> }
>
> cl*** myChild extends myParent {
> public function __construct() {
> parent::__construct();
> $this->type = 'Child';
> }
> public function myChild() {
> $this->__construct();
> }
> }
>
> $Child = new myChild();
> echo $Child->getType();
>
> ?>
>


ok, responded too quickly. Roman has it right with the settype method

This now works.

<?php

cl*** myParent {
private $type;
public function __construct() {
return $this;
}
public function myParent() {
return $this->__construct();
}
public function setType($value='') {
$this->type = $value;
}
public function getType() {
return $this->type;
}
}

cl*** myChild extends myParent {
public function __construct() {
parent::__construct();
$this->setType('Child');
return $this;
}
public function myChild() {
return $this->__construct();
}
}

$Child = new myChild();
echo $Child->getType();

?>
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: working with cl*** inheritance
Old
  (#7)
Gregory Beaver
Guest
 
Posts: n/a
Default Re: working with cl*** inheritance - 05-14-2007, 03:55 AM

Jeff Taylor wrote:
> Hey all, got a slight problem, where for some reasons my variables dont seem
> to be getting stored in the child cl***:
>
> e.g
>
> cl*** Parent
> {
> $private type;
>
> public function __construct()
> {
> }
>
> public function GetType()
> {
> return $this->type;
> }
> }
>
> cl*** Child implements Parent
> {
> $public function __construct()
>
>
> $this->type= 'Child';
> }
> }
>
> $Child= new Child();
> echo $Child->getType;
>
> Can u see any reason why the type would return null?


Hi Jeff,

Aside from your obvious parse errors, the main problem is that you are
incorrectly using "private." Since you wish to access $type from the
child cl***, you must use "protected" or declare a setter function as
Roman suggested. However, setter functions are far less efficient than
simply using protected:

<?php
cl*** whatever
{
protected $type = 'whatever';
}

cl*** Child extends whatever
{
public function __construct() {$this->type='Child';}
}
?>

It should be noted that if you simply want to store the cl*** name, a
better approach is to use get_cl***()

<?php
cl*** whatever {}
cl*** child {}
$a = new whatever;
$b = new child;
echo get_cl***($a), ' ', get_cl***($b);
?>

Of course, you may want to remove a prefix from the cl***name, in which
case you could also use a simple __get()/__set() declaration that
prevents accidental modification of object type:

<?php
cl*** prefix_mycl***
{
function __get($var)
{
if ($var == 'type') return str_replace('prefix_', '',
get_cl***($this));
}

function __set($var, $value)
{
if ($var == 'type') return; // ignore
}
}
cl*** prefix_child extends prefix_mycl*** {}

$a = new prefix_mycl***;
$b = new prefix_child;

echo $a->type , ' ' , $b->type;
$a->type = 5;
echo $a->type , ' ' , $b->type;
?>

Regards,
Greg
--
Experience the revolution, buy the PEAR Installer Manifesto
http://www.packtpub.com/book/PEAR-installer
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] working with cl*** inheritance
Old
  (#8)
Jeff Taylor
Guest
 
Posts: n/a
Default Re: [PHP] working with cl*** inheritance - 05-14-2007, 03:55 AM

Thanks everyone,

Gave me a much better understanding of it

"Jim Lucas" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED...
> Jim Lucas wrote:
> > Jeff Taylor wrote:
> >> Hey all, got a slight problem, where for some reasons my variables
> >> dont seem
> >> to be getting stored in the child cl***:
> >>
> >> e.g
> >>
> >> cl*** Parent

> > At least in PHP 5.2.1 on windows xp (for testing only), the cl*** name
> > Parent is a reserved cl*** name, you cannot define a cl*** by that name.
> >
> >> {
> >> $private type;
> >>
> >> public function __construct()
> >> {
> >> }
> >>
> >> public function GetType()
> >> {
> >> return $this->type;
> >> }
> >> }
> >>
> >> cl*** Child implements Parent
> >> {
> >> $public function __construct()

> > public should not be prefixed with '$'
> > you are missing your open '{' for this method
> >>

> >
> > You need to call
> > parent::__construct();
> >
> >>
> >> $this->type= 'Child';
> >> }
> >> }
> >>
> >> $Child= new Child();
> >> echo $Child->getType;

> > also, you are calling a method, not accessing a property.
> >
> > it should be
> > echo $Child->getType();
> >
> >>
> >> Can u see any reason why the type would return null?
> >>
> >> Thanks,
> >>
> >> Jeff
> >>
> >>

> > well, for starters, I think it is incorrect to have public and private
> > prefixed with a '$'. But then you have it on some, but not all of
> > them.....
> >
> > Besides that, does it give you an error? What does it return if it
> > doesn't return 'Child'??
> >
> > What version of PHP are you running on. there is a differnet way the
> > constuct() method gets called.
> >
> > public and private are not available in versions older than php 5
> >
> > in PHP version < 5 you have to have a method named the same as the cl***
> > that you are creating.
> >
> > Try this:
> > <?php
> >
> > cl*** myParent {
> > # private $type; #<--- With this, I cannot echo $type
> >
> > var $type; #<--- With this, I CAN echo $type
> >
> > public function __construct() {
> > }
> >
> > public function myParent() {
> > $this->__construct();
> > }
> > public function getType() {
> > echo $this->type;
> > }
> > }
> >
> > cl*** myChild extends myParent {
> > public function __construct() {
> > parent::__construct();
> > $this->type = 'Child';
> > }
> > public function myChild() {
> > $this->__construct();
> > }
> > }
> >
> > $Child = new myChild();
> > echo $Child->getType();
> >
> > ?>
> >

>
> ok, responded too quickly. Roman has it right with the settype method
>
> This now works.
>
> <?php
>
> cl*** myParent {
> private $type;
> public function __construct() {
> return $this;
> }
> public function myParent() {
> return $this->__construct();
> }
> public function setType($value='') {
> $this->type = $value;
> }
> public function getType() {
> return $this->type;
> }
> }
>
> cl*** myChild extends myParent {
> public function __construct() {
> parent::__construct();
> $this->setType('Child');
> return $this;
> }
> public function myChild() {
> return $this->__construct();
> }
> }
>
> $Child = new myChild();
> echo $Child->getType();
>
> ?>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] working with cl*** inheritance
Old
  (#9)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] working with cl*** inheritance - 05-14-2007, 03:56 AM

On Tue, March 20, 2007 3:42 am, Jeff Taylor wrote:
> Hey all, got a slight problem, where for some reasons my variables
> dont seem
> to be getting stored in the child cl***:
>
> e.g
>
> cl*** Parent
> {
> $private type;


Either you meant private $type; or I'd be shocked if this even got as
far as executing your subcl***...

Please paste real code instead of making us guess what you have.

> public function __construct()
> {
> }
>
> public function GetType()
> {
> return $this->type;
> }
> }
>
> cl*** Child implements Parent
> {
> $public function __construct()
>
>
> $this->type= 'Child';
> }
> }
>
> $Child= new Child();
> echo $Child->getType;
>
> Can u see any reason why the type would return null?


One possible answer:
If you are running PHP4, the constructor never gets called because
that's not how it was done in PHP4.

There are, of course, an infinite number of other possible answers, if
this isn't the actual code you are using.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top