Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.