Jump to content

how to use function in the constructor??


suami

Recommended Posts

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.

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

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)){

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.

 

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.