Instantiere clasa si apelare metoda intr-o functie

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
sterica
Mesaje: 285

Instantiere clasa si apelare metoda intr-o functie

Salutare,

Incerc sa instantiez o clasa si sa apelez la o metoda a aceleasi clase intr-o functie, insa nu reusesc sa definesc in parametrii functiei intantierea si metoda.

O sa expun doar ideile generale ale clasei si a functiei, sper sa fie suficient:

Cod: Selectaţi tot

Class DBConnection {
	....
	public function getQuery( $sql ) {
        	$stmt = $this->dbc->query( $sql );
        	$stmt->setFetchMode(PDO::FETCH_ASSOC);
        	return $stmt;
   	 }
}

function myfunction () {
	$sqlSelect = "";
	
	$database = new DBConnection($dbconfig);
	$rows = $database->getQuery($sqlSelect);
}
Multumesc!

MarPlo Mesaje: 4343
Salut
Transmite ca argumente la functie parametri necesari pt. clasa.
Poti folosi oricare din aceste doua metode date ca exemplu:
1.

Cod: Selectaţi tot

class clsTest {
  protected $prop ='';

  function __construct($prop){
    $this->prop = $prop;
  }

  public function method($str){
    return $this->prop .' / '. $str;
  }
}

//Receives $prop-argument for object-instance, $str-argument for method()
function fun($prop, $str){
  $ob = new clsTest($prop);
  $re = $ob->method($str);
  return $re .' / Text from function.';
}

$prop ='Prop for class.';
$str ='Text to method.';
echo fun($prop, $str); // Prop for class. / Text to method. / Text from function.
2.

Cod: Selectaţi tot

class clsTest {
  protected $prop ='';

  function __construct($prop){
    $this->prop = $prop;
  }

  public function method($str){
    return $this->prop .' / '. $str;
  }
}

//Receives $ob-object-instance, $str-argument for method()
function fun($ob, $str){
  $re = $ob->method($str);
  return $re .' / Text from function.';
}

$prop ='Prop for class.';
$ob = new clsTest($prop);

$str ='Text to method.';
echo fun($ob, $str); // Prop for class. / Text to method. / Text from function.

sterica Mesaje: 285
multumesc foarte mult de ajutor!

Subiecte similare