bdsafa Posted November 15, 2010 Share Posted November 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/ Share on other sites More sharing options...
AbraCadaver Posted November 15, 2010 Share Posted November 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1134625 Share on other sites More sharing options...
ManiacDan Posted November 15, 2010 Share Posted November 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1134629 Share on other sites More sharing options...
bdsafa Posted November 16, 2010 Author Share Posted November 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1134992 Share on other sites More sharing options...
ManiacDan Posted November 16, 2010 Share Posted November 16, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1134999 Share on other sites More sharing options...
bdsafa Posted November 16, 2010 Author Share Posted November 16, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1135054 Share on other sites More sharing options...
ManiacDan Posted November 16, 2010 Share Posted November 16, 2010 Never include files inside of a class declaration. You only need to include utt.php once, outside this function. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1135065 Share on other sites More sharing options...
bdsafa Posted November 16, 2010 Author Share Posted November 16, 2010 Never include files inside of a class declaration. You only need to include utt.php once, outside this function. -Dan yeah , two thumbs up for ManiacDan, you're the Man, i got it together. Quote Link to comment https://forums.phpfreaks.com/topic/218760-problem-with-the-classes/#findComment-1135178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.