objects of MyClass in array?

objects of MyClass in array?

am 07.01.2008 23:55:05 von devloop

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

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

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

file MyClass.inc:
-----------------------
class MyClass{
private $my_string_array=3Darray();

public function __construct(){

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

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

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

$x=3Dcount($my_string_array);
echo "number of elements=3D" . $x . "
\n";


echo "string0=3D ". $my_string_array[0] . "
\n";
echo "string1=3D ". $my_string_array[1] . "
\n";
echo "string2=3D ". $my_string_array[2] . "
\n";
echo "string3=3D ". $my_string_array[3] . "
\n";

}
}
?>

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

require_once("./MyClass.inc");

$s=3Dnew MyClass();

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

$q=3Dnew MyClass();
$q->fill_my_array();

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

?>

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

its an array
number of elements=3D0
string0=3D
string1=3D
string2=3D
string3=3D
finished MyClass with constructor

its an array
number of elements=3D0
string0=3D
string1=3D
string2=3D
string3=3D
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=B4s elemets are objects
of a class (e.g. MyOtherClass that one I still have to write it)?

Please help!

Re: objects of MyClass in array?

am 08.01.2008 00:16:42 von luiheidsgoeroe

On Mon, 07 Jan 2008 23:55:05 +0100, devloop wrote:

> 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:
> -----------------------
> > 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

Re: objects of MyClass in array?

am 08.01.2008 00:24:46 von devloop

Thank you soooo much! That=B4s 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

Re: objects of MyClass in array?

am 08.01.2008 00:30:57 von luiheidsgoeroe

On Tue, 08 Jan 2008 00:24:46 +0100, devloop wrote:

> 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

Re: objects of MyClass in array?

am 08.01.2008 00:31:57 von Steve

"devloop" wrote in message
news:a21e6087-932a-4683-95d0-3d1d65d7288e@s19g2000prg.google groups.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.?

Re: objects of MyClass in array?

am 08.01.2008 00:43:38 von luiheidsgoeroe

On Tue, 08 Jan 2008 00:31:57 +0100, Steve wrote:
> "devloop" wrote in message
> news:a21e6087-932a-4683-95d0-3d1d65d7288e@s19g2000prg.google groups.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

Re: objects of MyClass in array?

am 08.01.2008 01:04:54 von Steve

"Rik Wasmus" wrote in message
news:op.t4k2u0075bnjuv@metallium.lan...
> On Tue, 08 Jan 2008 00:31:57 +0100, Steve wrote:
>> "devloop" wrote in message
>> news:a21e6087-932a-4683-95d0-3d1d65d7288e@s19g2000prg.google groups.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;
> }
> }

Re: objects of MyClass in array?

am 08.01.2008 01:10:18 von luiheidsgoeroe

On Tue, 08 Jan 2008 01:04:54 +0100, Steve wrote:
> "Rik Wasmus" wrote in message
> news:op.t4k2u0075bnjuv@metallium.lan...
>> On Tue, 08 Jan 2008 00:31:57 +0100, Steve wrote:
>>> "devloop" wrote in message
>>> news:a21e6087-932a-4683-95d0-3d1d65d7288e@s19g2000prg.google groups.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

Re: objects of MyClass in array?

am 08.01.2008 04:35:05 von Steve

"Rik Wasmus" wrote in message
news:op.t4k33gji5bnjuv@metallium.lan...
> On Tue, 08 Jan 2008 01:04:54 +0100, Steve wrote:
>> "Rik Wasmus" wrote in message
>> news:op.t4k2u0075bnjuv@metallium.lan...
>>> On Tue, 08 Jan 2008 00:31:57 +0100, Steve wrote:
>>>> "devloop" wrote in message
>>>> news:a21e6087-932a-4683-95d0-3d1d65d7288e@s19g2000prg.google groups.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.

Re: objects of MyClass in array?

am 08.01.2008 08:01:51 von Michael Fesser

..oO(Steve)

>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.

I find it quite useful. In my own framework for example I have a class
TSitemap that holds a representation of all the documents on a website.
It contains a tree-like structure of simple page objects, which store
the name, title, URL path, description, access permissions etc. for
nearly every page. This sitemap is mainly used to automatically generate
nav bars (main menu, sub menus, breadcrumbs etc.) and such things, but
from time to time it's also necessary to directly access a particular
page object from the tree or to iterate over all pages, regardless of
their relation to other pages. To achieve this I simply implement the
ArrayAccess and Iterator interfaces in the TSitemap class.

Micha

Re: objects of MyClass in array?

am 08.01.2008 15:06:15 von Steve

"Michael Fesser" wrote in message
news:a676o3tif3o2rsja45dlc10dphncslb24m@4ax.com...
> .oO(Steve)
>
>>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.
>
> I find it quite useful. In my own framework for example I have a class
> TSitemap that holds a representation of all the documents on a website.
> It contains a tree-like structure of simple page objects, which store
> the name, title, URL path, description, access permissions etc. for
> nearly every page. This sitemap is mainly used to automatically generate
> nav bars (main menu, sub menus, breadcrumbs etc.) and such things, but
> from time to time it's also necessary to directly access a particular
> page object from the tree or to iterate over all pages, regardless of
> their relation to other pages. To achieve this I simply implement the
> ArrayAccess and Iterator interfaces in the TSitemap class.

hmmm...hadn't thought of doing that. do you build that from a db each time a
page is visited or do you build it once and then put it into a session?

Re: objects of MyClass in array?

am 08.01.2008 15:20:07 von Michael Fesser

..oO(Steve)

>"Michael Fesser" wrote in message
>news:a676o3tif3o2rsja45dlc10dphncslb24m@4ax.com...
>>
>> I find it quite useful. In my own framework for example I have a class
>> TSitemap that holds a representation of all the documents on a website.
>> It contains a tree-like structure of simple page objects, which store
>> the name, title, URL path, description, access permissions etc. for
>> nearly every page. This sitemap is mainly used to automatically generate
>> nav bars (main menu, sub menus, breadcrumbs etc.) and such things, but
>> from time to time it's also necessary to directly access a particular
>> page object from the tree or to iterate over all pages, regardless of
>> their relation to other pages. To achieve this I simply implement the
>> ArrayAccess and Iterator interfaces in the TSitemap class.
>
>hmmm...hadn't thought of doing that. do you build that from a db each time a
>page is visited or do you build it once and then put it into a session?

Currently it's rebuilt on every request. Some parts of the sitemap come
from a DB to serve the rather static pages (their content is stored in
flat files on disk and just put into a global page template when the
page is requested), other parts are added by some components of the
framework. My 'admin' module for example adds its own section to the
sitemap as the entry point for all the admin components and plugins.

If this should become a bottleneck one day, then I might implement some
kind of caching, but currently the performance is OK. The bottlenecks
are somewhere else.

Micha

Re: objects of MyClass in array?

am 08.01.2008 21:01:44 von devloop

O.K., I=B4ll start with foreach and keep in mind the perhaps-performance-
problem.
Thanks
devloop