suami Posted November 18, 2008 Share Posted November 18, 2008 Hello, I'm writing a class and I want to use a function that checks something in the constructor Can I do that ? and How? Thanks suami. Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/ Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 Of course. What exactly do you want to check? class tester { public function __construct($input){ if($this->check($input)){ echo "Passed check."; }else{ echo "Failed check."; } } public function check($value){ if(str_replace(" ", "", $value) == ""){ return false; }else{ return true; } } } $test = new tester("This will pass!"); I'm not exactly sure what you're after though. Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692671 Share on other sites More sharing options...
suami Posted November 18, 2008 Author Share Posted November 18, 2008 function Table($str) { $this -> table_in_string = $str; // create_table($this -> $table_in_string); //after initailze all the params creating the table. $i=1; $split_string = array(); $split_data_cells = ""; $split_string = explode("</tr>", $this -> table_in_string); //For each row in the table foreach($split_string as $this -> table_in_string){ if($i != sizeof($split_string)){ $row = substr($this -> table_in_string, strpos($this -> table_in_string, "<tr>")+4); if(is_substr("<th>", $row)){ $split_data_cells = explode("</th>", $row); $open_tag = "<th>"; } else{ $split_data_cells = explode("</td>", $row); $open_tag = "<td>"; } $j = 1; $i++; foreach($split_data_cells as $data_cell){ $data_cell = substr($data_cell, strpos($data_cell, $open_tag)+strlen($open_tag)); } } } $this -> tr = $split_string; $this -> td = $split_data_cells; } ### and this is the error i get Fatal error: Call to undefined function is_substr() in thanks again Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692687 Share on other sites More sharing options...
trq Posted November 18, 2008 Share Posted November 18, 2008 Where is is_substr() defined? Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692691 Share on other sites More sharing options...
suami Posted November 18, 2008 Author Share Posted November 18, 2008 in the class but below the constructor.. Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692692 Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 If is_substr() is a method then you aren't calling it correctly. You're just calling it as is_substr(), perhaps your missing the $this-> from it, to call it as a method of your class. ??? EDIT: In that case, you are missing $this-> from the beginning of the function. if(is_substr("<th>", $row)){ Should be: if($this->is_substr("<th>", $row)){ Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692693 Share on other sites More sharing options...
suami Posted November 18, 2008 Author Share Posted November 18, 2008 this is the function which i define after the constructor function is_substr($needle, $haystack){ $pos = strpos($haystack, $needle); if ($pos === false) { return false; } else { return true; } } sorry but I didn't understand you, did you mean I should call it like this: if(this->is_substr("<th>", $row)){ } ??? thanks. Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692701 Share on other sites More sharing options...
suami Posted November 18, 2008 Author Share Posted November 18, 2008 if i do that it says Parse error: syntax error, unexpected T_OBJECT_OPERATOR in Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692703 Share on other sites More sharing options...
trq Posted November 18, 2008 Share Posted November 18, 2008 It should be called... if ($this->is_substr("<th>", $row)) { Link to comment https://forums.phpfreaks.com/topic/133184-how-to-use-function-in-the-constructor/#findComment-692754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.