By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

objects of MyClass in array?

P: n/a
Hi,
I am a JAVA developer who just started with php5 (on a WAMPP with
php5.2.1).

I┤ve the following problem with a little class (I wanted to put
objects into the array later but even with simple strings it doesn┤t
work!!!!!!!!!).

The output doesn┤t show any letters/strings when I call the method
toHtml().

file MyClass.inc:
-----------------------
<?php
class MyClass{
private $my_string_array=array();

public function __construct(){

$my_string_array[]="A";
$my_string_array[]="B";
$my_string_array[]="C";
$my_string_array[]="D";
}

public function fill_my_array(){
$my_string_array[0]="A";
$my_string_array[1]="B";
$my_string_array[2]="C";
$my_string_array[3]="D";
}
public function toHtml(){

if( is_array($this->my_string_array))
{
echo "its an array <br>\n";
}

$x=count($my_string_array);
echo "number of elements=" . $x . "<br>\n";
echo "string0= ". $my_string_array[0] . "<br>\n";
echo "string1= ". $my_string_array[1] . "<br>\n";
echo "string2= ". $my_string_array[2] . "<br>\n";
echo "string3= ". $my_string_array[3] . "<br>\n";

}
}
?>

file test.php
-------------------

<?php
require_once("./MyClass.inc");

$s=new MyClass();

$s->toHtml();
echo "finished MyClass with constructor<br>\n";

$q=new MyClass();
$q->fill_my_array();

$q->toHtml();
echo "finished MyClass with extra fill function<br>\n";

?>

Output (as not excepted):
--------------------------------------

its an array
number of elements=0
string0=
string1=
string2=
string3=
finished MyClass with constructor

its an array
number of elements=0
string0=
string1=
string2=
string3=
finished MyClass with extra fill function
What the hell is wrong????????????????????????????????????

optional when problem solved:

How do you have to write MyClass when the array┤s elemets are objects
of a class (e.g. MyOtherClass that one I still have to write it)?

Please help!

Jan 7 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Mon, 07 Jan 2008 23:55:05 +0100, devloop <an********@gmx.dewrote:
Hi,
I am a JAVA developer who just started with php5 (on a WAMPP with
php5.2.1).

I┬┤ve the following problem with a little class (I wanted to put
objects into the array later but even with simple strings it doesn┬┤t
work!!!!!!!!!).

The output doesn┬┤t show any letters/strings when I call the method
toHtml().

file MyClass.inc:
-----------------------
<?php
class MyClass{
private $my_string_array=array();

public function __construct(){

$my_string_array[]="A";
You should use:
$this->my_string_array[] = 'A';
$my_string_array is only a variable in the scope of the function and will
be discarded as soon as the function finishes.
optional when problem solved:

How do you have to write MyClass when the array┬┤s elemets are objects
of a class (e.g. MyOtherClass that one I still have to write it)?
Just add an object to the array instead of a string, makes no difference
(allthough you may want to look at the Iterator interface)
--
Rik Wasmus
Jan 7 '08 #2

P: n/a
Thank you soooo much! That┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?
Any hint for me for reading s.th. about that (I loved then in JAVA,
they made life easier).

Thanks again

devloop

Jan 7 '08 #3

P: n/a
On Tue, 08 Jan 2008 00:24:46 +0100, devloop <an********@gmx.dewrote:
Thank you soooo much! That┬┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?
See: http://nl2.php.net/manual/en/ref.spl.php
Just make sure you explicitly state you class implements ArrayIterator
--
Rik Wasmus
Jan 7 '08 #4

P: n/a

"devloop" <an********@gmx.dewrote in message
news:a2**********************************@s19g2000 prg.googlegroups.com...
Thank you soooo much! That┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?

===========

no. there is very little strong typing in php and an array has the
enumerable interface. anything you put in an array can be iterated with
foreach

===========

Any hint for me for reading s.th. about that (I loved then in JAVA,
they made life easier).

===========

what's s.th.?
Jan 7 '08 #5

P: n/a
On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example.comwrote:
"devloop" <an********@gmx.dewrote in message
news:a2**********************************@s19g2000 prg.googlegroups.com...
Thank you soooo much! That┬┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?

===========

no.
Yes, Iterators exist, as the are an interface for a class.
there is very little strong typing in php
Which has to do with?
and an array has the
enumerable interface. anything you put in an array can be iterated with
foreach
Yes, however, creating an object as an ArrayObject/implementing
ArrayIterator, and private variables, will give you the possibility for
type hinting:
class MyCollection implements ArrayIterator{
...
private $values = array();
public function add(MyParticularObject $value){
$this->values[] = $value;
}
}
--
Rik Wasmus
Jan 7 '08 #6

P: n/a

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example.comwrote:
>"devloop" <an********@gmx.dewrote in message
news:a2**********************************@s19g200 0prg.googlegroups.com...
Thank you soooo much! That┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?

===========

no.

Yes, Iterators exist, as the are an interface for a class.
should have been more specific. 'no' was to the first question...does he
have to do anything special. do they exist? clearly. the only time he has to
mess with it though is if he wants to customize a class of his own so that
it is directly enumerable.
>there is very little strong typing in php

Which has to do with?
usually people customize a class to be enumerable is to get performance
benefits in storing a specific type rather than a variant, default
collection. that's what i meant. the second most common is functional
clarity for the caller.
>and an array has the
enumerable interface. anything you put in an array can be iterated with
foreach

Yes, however, creating an object as an ArrayObject/implementing
ArrayIterator, and private variables, will give you the possibility for
type hinting:
true...but, i don't have that ide if 'intellisense' is what you mean. :)

as for the code below, MyParticularObject can 'hint' a function param type
anywhere...even a proc function. so, i'm not sure what you mean.
>
class MyCollection implements ArrayIterator{
...
private $values = array();
public function add(MyParticularObject $value){
$this->values[] = $value;
}
}


Jan 8 '08 #7

P: n/a
On Tue, 08 Jan 2008 01:04:54 +0100, Steve <no****@example.comwrote:
"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
>On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example.comwrote:
>>"devloop" <an********@gmx.dewrote in message
news:a2**********************************@s19g20 00prg.googlegroups.com...
Thank you soooo much! That┬┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?

===========

no.

Yes, Iterators exist, as the are an interface for a class.

should have been more specific. 'no' was to the first question..
Ah, your answer was confusing in that aspect indeed/
>>there is very little strong typing in php

Which has to do with?

usually people customize a class to be enumerable is to get performance
benefits in storing a specific type rather than a variant, default
collection. that's what i meant. the second most common is functional
clarity for the caller.
>>and an array has the
enumerable interface. anything you put in an array can be iterated with
foreach

Yes, however, creating an object as an ArrayObject/implementing
ArrayIterator, and private variables, will give you the possibility for
type hinting:

true...but, i don't have that ide if 'intellisense' is what you mean. :)
Huh? ide? intellisense? I see no connection to an IDE, and intellisense
you'll have to explain to me.
as for the code below, MyParticularObject can 'hint' a function param
type
anywhere...even a proc function. so, i'm not sure what you mean.
That was a response to your 'typing' remark, guessing at what you meant,
an illustration how you could force an array object to hold only one type
of data/class in it's 'collection'/array
--
Rik Wasmus
Jan 8 '08 #8

P: n/a

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 08 Jan 2008 01:04:54 +0100, Steve <no****@example.comwrote:
>"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
>>On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example.comwrote:
"devloop" <an********@gmx.dewrote in message
news:a2**********************************@s19g2 000prg.googlegroups.com...
Thank you soooo much! That┤s it! I would have cost me years to find
that as in JAVA you may write this or leave it!

Do I have to add any include line to use iterator class or
collections? Do they exist in php?

===========

no.

Yes, Iterators exist, as the are an interface for a class.

should have been more specific. 'no' was to the first question..

Ah, your answer was confusing in that aspect indeed/
i'll try harder to be more clear next time. :)
>>>there is very little strong typing in php

Which has to do with?

usually people customize a class to be enumerable is to get performance
benefits in storing a specific type rather than a variant, default
collection. that's what i meant. the second most common is functional
clarity for the caller.
>>>and an array has the
enumerable interface. anything you put in an array can be iterated with
foreach

Yes, however, creating an object as an ArrayObject/implementing
ArrayIterator, and private variables, will give you the possibility for
type hinting:

true...but, i don't have that ide if 'intellisense' is what you mean. :)

Huh? ide? intellisense? I see no connection to an IDE, and intellisense
you'll have to explain to me.
it's also called auto-completion...start typing a variable name in and you
have to option completing it by selecting from a list...or, you can see the
next param that's supposed to be entered if typing a function - complete
with the param data type. that was one kind of 'hinting' i was guessing at.
just wasn't sure that was what you meant. i know it wasn't now.
>as for the code below, MyParticularObject can 'hint' a function param
type
anywhere...even a proc function. so, i'm not sure what you mean.

That was a response to your 'typing' remark, guessing at what you meant,
an illustration how you could force an array object to hold only one type
of data/class in it's 'collection'/array
yes, that's what i meant. as it is though, i haven't really had a need to
implement an interator on a custom class in php. arrays work fine for just
about everything in php. i did, however, enjoy reading the article you
referenced about doing just that - custom classes implementing iterators.
Jan 8 '08 #9

P: n/a
O.K., I┤ll start with foreach and keep in mind the perhaps-performance-
problem.
Thanks
devloop
Jan 8 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.