OO Php problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TimField
Forum Newbie
Posts: 6
Joined: Mon Sep 06, 2004 9:37 pm
Location: New Zealand

OO Php problems

Post by TimField »

:arrow: Sami | Use

Code: Select all

&
for posting code in the forums. Thanks.[/url][/size][/color]

Hmmm this is a tricky one
Im building a content management system, which is now working in php5. However Im adding it to a site im doing where the server runs php 4.3.8.
I'll try and explain this as best I can.

I have a page object, a block object and and info object.
pages hold blocks which hold info (text and stuff).

inside the block object I can tell all its info objects to setEdit

Code: Select all

<?php
class block (
...
function editContent(){
		echo('im telling the info''s ted');
		foreach($this->infos as $info){
		$info->setEdit();}
	}
...)
?>
which calls this function in an info object

Code: Select all

<?php
class info(
...

function setEdit(){
...
$this->edit = true;
...
}
?>
ok now that works fine if I echo here its true. but later in the info object I test for $this->edit and its not set. Infact if I try to echo it I get nothing.

as I said this all works fine with php5.

any ideas?

Thanks
Contributers get free access to the completed product!!
:D (well actually anyone can, but at least ill help them install it! ;) )


:arrow: Sami | Use

Code: Select all

&
for posting code in the forums. Thanks.[/url][/size][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not too sure here, but at least in 4, foreach gives you a copy of the variables contained in an array/object, not a reference. It may be the same with 5..
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

you do have var $edit; in your code right?
User avatar
TimField
Forum Newbie
Posts: 6
Joined: Mon Sep 06, 2004 9:37 pm
Location: New Zealand

Post by TimField »

Yup had to double check that :wink:
But var $edit is indeed set.
So it's not that simply im affraid, must be somthing specific to < php5 because as I said with 5 it works fine
User avatar
TimField
Forum Newbie
Posts: 6
Joined: Mon Sep 06, 2004 9:37 pm
Location: New Zealand

Post by TimField »

Just reading the comment

RE
not too sure here, but at least in 4, foreach gives you a copy of the variables contained in an array/object, not a reference. It may be the same with 5..

Hmmm so this could be where the problem lies.

So later when my block object goes back through its array of info objects it takes a fresh copy ? (but it wouldnt reset it's vars would it :? ) whereas in php 5 it references the already existing copy and the vars are as they were.

but surley php4 handles objects better than that otherwise many would have come accross this problem in the past.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php4 pretty much always copies, unless explicitly told to reference. Foreach doesn't support referencing. If it's a numeric array, a simple for loop will handle it a lot easier. If it's a named array, extracting the name list into a numeric array ([php_man]array_keys[/php_man]) you can do a very similar thing.
User avatar
TimField
Forum Newbie
Posts: 6
Joined: Mon Sep 06, 2004 9:37 pm
Location: New Zealand

Post by TimField »

Thanks feyd

Indeed that was the problem its now working. I changed the code to

foreach ($this->infos as $key=>$copy){
$i =& $this->infos[$key];
$i->setEdit();
}

um is you mentioned array keys but i wasnt sure on the syntax for those, if youve got the time how would an array_keys soloution look.

Many Thanks
Tim
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$keys = array_keys($named_array);
for($x = 0, $y = sizeof($keys); $x < $y; $x++) $this->infos[$named_array[$x]]->setEdit();
Post Reply