[PHP] Passing a class through an array
Hey all, I'm very new to programming and PHP. I want to pass instances of a class through an array, and then use a foreach later to grab details of each class at once... but it returns a fatal error Code: $me = new Toon('Toon1','1') $him = new Toon('Toon2','0') $characterListing = array($me, $him); foreach ($characterListing as $name1) { if ($name1->GetIsPlayer()) { echo "Name: ".$name1->$name;// etc etc etc } } the error I get is: Fatal error: Cannot access empty property in c:\Inetpub\wwwroot\MyProjects\SecondOffering\htmlFns.php on line 140 Can anyone help? Thanks, Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Troubles from the newb again
Is it possible to just add a value to the existing? here is my current script: foreach ($newarray as $name) { $newarray[$name] = $name->currentInitiative = rand(1,$name->GetInitiative())); } // sort array by ranking (initiative) sort($newarray,SORT_NUMERIC); can I change the $newarray[$name] to something to represent the current element and just add the value to it? I might be able to work around it then ""Jeff Taylor"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hey everyone, > Newb back again - Im trying to populate my arrays, but getting this error > again: > Warning: Illegal offset type in > c:\Inetpub\wwwroot\MyProjects\SecondOffering\myFuncs.php on line 242 > > Code: >$newarray = array(); >foreach ($array as $name) >{ > if ($name->currentHP >0) > { > $newarray[$name]; > } >} -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Troubles from the newb again
Yeah I have, but what I dont understand is that the value of $name is an object, and was accepted in the original array as the index: the original array was: $me = new Toon(,xxx,xxx,,xxx,etc,etc,etc) $him = new Toon(xxx,xx,x,etc,etc,etc,etc) $array=array($me,$him) So this new array, I want to isolate those toons still alive: if alive then add toon to new array Is this possible in anyway? The reason why I dont want it to put it in a value: foreach ($newarray as $name) { if (in_array($name,$characters)) { $newarray[$name] = 1; } else { $newarray[$name] = 2; } } Sorry for the spam everyone "Robert Cummings" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On Sat, 2007-03-10 at 15:31 +1030, Jeff Taylor wrote: > > Hey everyone, > > Newb back again - Im trying to populate my arrays, but getting this error > > again: > > Warning: Illegal offset type in > > This usually means that you are using a non-scalar value as an index. > Use var_dump( $array ) to see what's in there. You will probably find > one of the following: an array, and object, or the null value. > > Cheers, > Rob. > -- > .. > | InterJinn Application Framework - http://www.interjinn.com | > :: > | An application and templating framework for PHP. Boasting | > | a powerful, scalable system for accessing system services | > | such as forms, properties, sessions, and caches. InterJinn | > | also provides an extremely flexible architecture for | > | creating re-usable components quickly and easily. | > `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Passing a class through an array
Ahh the problem was that I was using $name1->$name instead of $name1->name to call the variable. Thanks though :) > If line 140 is the $name1->name part, then you probably haven't > written your constructor correctly to cram 'Toon1' into the name > field... > > > On Thu, March 8, 2007 11:51 pm, Jeff Taylor wrote: > > Hey all, > > I'm very new to programming and PHP. > > I want to pass instances of a class through an array, and then use a > > foreach > > later to grab details of each class at once... but it returns a fatal > > error > > > > Code: > > > > $me = new Toon('Toon1','1') > > $him = new Toon('Toon2','0') > > > >$characterListing = array($me, $him); > > > >foreach ($characterListing as $name1) > >{ > > if ($name1->GetIsPlayer()) > > { > > echo "Name: ".$name1->$name;// etc etc etc > > } > >} > > > > > > > > the error I get is: Fatal error: Cannot access empty property in > > c:\Inetpub\wwwroot\MyProjects\SecondOffering\htmlFns.php on line 140 > > > > Can anyone help? > > > > Thanks, Jeff > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > -- > 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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] working with class inheritance
Hey all, got a slight problem, where for some reasons my variables dont seem to be getting stored in the child class: e.g class Parent { $private type; public function __construct() { } public function GetType() { return $this->type; } } class 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: working with class inheritance
OOPS!... typo Please replace implements with extends: class Child extends Parent Sorry about that ""Jeff Taylor"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hey all, got a slight problem, where for some reasons my variables dont seem > to be getting stored in the child class: > > e.g > > class Parent > { > $private type; > > public function __construct() > { > } > >public function GetType() >{ > return $this->type; > } > } > > class 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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: working with class inheritance
I forgot to mention that I have some __construct operations in the parent class, and I want to add to those (and overwrite sometimes) in the child class __construct i.e Parent class __construct $this->foo = 'Bar'; $this->foo2 = '10'; Child class __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 PROTECTED]> wrote in message news:[EMAIL PROTECTED] > OOPS!... typo > > Please replace implements with extends: > > class Child extends Parent > > Sorry about that > ""Jeff Taylor"" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Hey all, got a slight problem, where for some reasons my variables dont > seem > > to be getting stored in the child class: > > > > e.g > > > > class Parent > > { > > $private type; > > > > public function __construct() > > { > > } > > > >public function GetType() > >{ > > return $this->type; > > } > > } > > > > class 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 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] working with class inheritance
Thanks everyone, Gave me a much better understanding of it "Jim Lucas" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 class: > >> > >> e.g > >> > >> class Parent > > At least in PHP 5.2.1 on windows xp (for testing only), the class name > > Parent is a reserved class name, you cannot define a class by that name. > > > >> { > >> $private type; > >> > >> public function __construct() > >> { > >> } > >> > >>public function GetType() > >>{ > >> return $this->type; > >> } > >> } > >> > >> class 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 class > > that you are creating. > > > > Try this: > > > > > class 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; > > } > > } > > > > class 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. > > > class 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; > } > } > > class 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(); > > ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php