mama php oop use karala app ekak hadanawa.mama php frameworks,core php use karala thibbata custom framework ekak ekka wada karala naa oop walin.mata thiyana awula menna mekai.mage db class eka wada karanawa.thawa class ekak thiyanawa user kiyala.meka thamai eke code eka,
include karala thiyana db.php file eke thamai config kiyana class eka thiyenne.eka dependency injection ekak widiyata user class eke constructor ekata pass karanawa.dan menna mekedi thamai issue eka enne.mama user_operation.php kiyana file eke me class eka call karala data ganna yanawa.meka thamai code eka,
menna me error eka print wenawa php page eke,
"Catchable fatal error: Argument 1 passed to user::__construct() must be an instance of config, none given, called in C:\xampp\htdocs\php_app\Admin\user_operation.php on line 474 and defined in C:\xampp\htdocs\php_app\Admin\classes\user.php on line 11"
PHP:
<?php
require_once ("db.php");;
class user{
public $name;
public $age;
public $contact;
public $config;
public function __construct(config $config){
$this->config=$config;
}
public function setUser($name,$age,$contact){
$this->name=$name;
$this->age=$age;
$this->contact=$contact;
}
public function getUsers(){
// We need to have 1 config object with all data in.
$config = new config("localhost", "root", "", "app_db", "", "mysqli"); //type in your data here…
// Now we need to have access to the db class, we uses the config object to configure the db object.
$db = new db($config);
// We can now open the connection to the database.
$db->openConnection();
// If your config details are right, we are now connected to a database, lets test the connection before we run queries.
$are_we_online = $db->pingServer();
// The variable $are_we_online should be true (or 1) if we are connected to the server.
//echo "Are we online: " . $are_we_online; // prints 0 or 1.
// Let us run a query.
$sql = $db->query("SELECT * FROM {users}");
// The variable $sql will now hold the data returned from the database, we can now work with it.
// Does it have rows ?
/*$hasRows = $db->hasRows($sql);
echo "Does it have rows: " . $hasRows; // prints 0 or 1 (true or false).
// How many rows does it have.
$countRows = $db->countRows($sql);
echo "How many rows: " . $countRows; // returns the number of rows.*/
// We can get the data from the fetch_assoc function.
//$result = $db->fetchAssoc($sql);
// We can get the data from the fetch_array function.
$result = $db->fetchArray($sql);
//We can even print out the latest used query:
//echo $db->lastQuery();
return $result;
}
}
include karala thiyana db.php file eke thamai config kiyana class eka thiyenne.eka dependency injection ekak widiyata user class eke constructor ekata pass karanawa.dan menna mekedi thamai issue eka enne.mama user_operation.php kiyana file eke me class eka call karala data ganna yanawa.meka thamai code eka,
PHP:
require_once("classes/users.php");
$userObj=new user();
$userResults=$userObj->getUsers();
foreach ($userResults as $specificUser){
echo $specificUser['name'];
echo '</br>';
}
menna me error eka print wenawa php page eke,
"Catchable fatal error: Argument 1 passed to user::__construct() must be an instance of config, none given, called in C:\xampp\htdocs\php_app\Admin\user_operation.php on line 474 and defined in C:\xampp\htdocs\php_app\Admin\classes\user.php on line 11"

owu mama print karanne naha machan view ekedi thamai print karanne
$this->config eka try karala balannan machan.uda daala thiyana anith widi try kala eth wada naha

