473,788 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arra y=array();

public function __construct(){

$my_string_arra y[]="A";
$my_string_arra y[]="B";
$my_string_arra y[]="C";
$my_string_arra y[]="D";
}

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

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

$x=count($my_st ring_array);
echo "number of elements=" . $x . "<br>\n";
echo "string0= ". $my_string_arra y[0] . "<br>\n";
echo "string1= ". $my_string_arra y[1] . "<br>\n";
echo "string2= ". $my_string_arra y[2] . "<br>\n";
echo "string3= ". $my_string_arra y[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 2373
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_arra y=array();

public function __construct(){

$my_string_arra y[]="A";
You should use:
$this->my_string_arra y[] = 'A';
$my_string_arra y 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******** *************** ***********@s19 g2000prg.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.?
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******** *************** ***********@s19 g2000prg.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(MyParticula rObject $value){
$this->values[] = $value;
}
}
--
Rik Wasmus
Jan 7 '08 #6

"Rik Wasmus" <lu************ @hotmail.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example .comwrote:
>"devloop" <an********@gmx .dewrote in message
news:a2******* *************** ************@s1 9g2000prg.googl egroups.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, MyParticularObj ect 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(MyParticula rObject $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.comwro te in message
news:op******** *******@metalli um.lan...
>On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example .comwrote:
>>"devloop" <an********@gmx .dewrote in message
news:a2****** *************** *************@s 19g2000prg.goog legroups.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
ArrayIterato r, 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, MyParticularObj ect 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.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 08 Jan 2008 01:04:54 +0100, Steve <no****@example .comwrote:
>"Rik Wasmus" <lu************ @hotmail.comwro te in message
news:op******* ********@metall ium.lan...
>>On Tue, 08 Jan 2008 00:31:57 +0100, Steve <no****@example .comwrote:
"devloop" <an********@gmx .dewrote in message
news:a2***** *************** **************@ s19g2000prg.goo glegroups.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
collection s? 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...st art 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, MyParticularObj ect can 'hint' a function param
type
anywhere...eve n 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
2581
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
7
11565
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 my objects? I tried MyClass myObject(100); and
10
12231
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
1953
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 = new Airplane("N12345"); Actually, I create an array of Airplane-pointers first and then create the
2
2043
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
1355
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 based on the values of one of the object's members. Here is a bit of code to illustrate my setup: Object construction and insertion: Dim entity As New entityobjects(entitynamenodelist(i).InnerXml, typenodelist(i).InnerXml, ...)
1
3251
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
1817
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
6351
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 works fine (see below). So what is the problem? I get a null reference on the line below at *!&!* "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.? RL
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.