Jump to content

Problem with the classes


bdsafa

Recommended Posts

Yo, everyone.

 

I got a problem with the class i wrote for my coding, I need to use the class for every data that will show up. for that i used a 'for' cycle, but it seems i get only the first one of the cycle right. well the problem seems that the class can only be included once and for the rest, I got a header problem.

 

Is there a way to actually use classes in a cycle? Thanks in Advance.

Link to comment
https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/
Share on other sites

Yo, everyone.

 

I got a problem with the class i wrote for my coding, I need to use the class for every data that will show up. for that i used a 'for' cycle, but it seems i get only the first one of the cycle right. well the problem seems that the class can only be included once and for the rest, I got a header problem.

 

Is there a way to actually use classes in a cycle? Thanks in Advance.

 

You'll have to post some code.  You can only define a class once, just like a function, but after it's defined you can use it as many times as you want.

You'll have to post some code.  You can only define a class once, just like a function, but after it's defined you can use it as many times as you want.

It's also called a LOOP, not a cycle.

 

Classes should always be defined outside any control structures (preferably in their own files) and simply instantiated any time they're needed.

 

-Dan

 

Yeah, loop  , sorry about that :)

 

by the way , i know that its only need to include once, but the way i need to use it is a little different, look :

 

include_once('datacorrection.php');
$file = file("sampletxt.txt");
foreach ($file as $line) { $data = explode('|',$line);
$title = $data[0];
datacorrection($title);
echo $title; }

you see? i can't simply put the class out of the loop. if i do, nothing will be corrected.

You may be surprised to learn that there's no classes in this code snippet.  Do you maybe mean functions?  What is the content of datacorrection.php?

 

I exactly know that its a function, here is the datacorrection.php :

function datacorrection(&$str)
{
include('utt.php');
    $text = explode("\n", $str);
$str = array();
foreach($text as $line){
	$chars = utt::utt(utt::uttStringToArray($line), 'R');
	$line = '';
	foreach($chars as $char){
		$line .= utt::unichr($char);
	}
	$str[] = $line;
}
$str = implode("\n", $str);
}
?>

 

and utt.php starts with a class named utt. you see?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.