Page 1 of 1

OO Php problems

Posted: Mon Sep 06, 2004 9:49 pm
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]

Posted: Mon Sep 06, 2004 10:13 pm
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..

Posted: Tue Sep 07, 2004 12:38 pm
by PrObLeM
you do have var $edit; in your code right?

Posted: Tue Sep 07, 2004 5:26 pm
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

Posted: Tue Sep 07, 2004 5:32 pm
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.

Posted: Tue Sep 07, 2004 8:54 pm
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.

Posted: Wed Sep 08, 2004 10:16 pm
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

Posted: Thu Sep 09, 2004 1:14 am
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();