473,383 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

objects of MyClass in array?

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
9 2350
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
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
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

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

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

"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
O.K., I´ll start with foreach and keep in mind the perhaps-performance-
problem.
Thanks
devloop
Jan 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
7
by: b83503104 | last post by:
Previously, when my constructor had no arguments, I used this to declare my objects: MyClass myObject; Now, my constructor has arguments (MyClass::MyClass(int someVariable)), how do I declare...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
2
by: Michele | last post by:
Hi, I have trouble with an array of objects! I declared a class like this: Public Class myClass Public X As String Public Y As Integer Public Z As String
7
by: Derek Martin | last post by:
Hi there, I've asked before but never got an answer that could get me completely to where I was heading. I have an arraylist that contains defined objects and I would like to sort this arraylist...
1
by: Behzad | last post by:
hi, i want to know what happens to objects in an array if i redeclare the array.. look at the following code: ////my code public MyClass myarray=new MyClass;
6
by: Jonas Huckestein | last post by:
hello, somehow i can't figure out, how to overload the operator for a referenced object. if i have class MyClass { int operator(int i) { return 1; }; };
3
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.