PHP OOP issue

pen02x01

Member
Dec 9, 2015
17
0
0
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,

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"
 

DooA

Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    public function __construct(config $config){
    }

    කෝඩ් එකේ හැටියට user class එකේ constructor එකට parameter එකක් pass කරන්න ඕනේ. උබ user instance එක හදන කොට pass කරලා na.

    PHP:
    //this config var need to pass into user ins
    $config = new Config();
    $userObj=new user($config); 
    
    
                $userResults=$userObj->getUsers(); 
                foreach ($userResults as $specificUser){ 
                    echo $specificUser['name']; 
                    echo '</br>'; 
                }

    එහම නැතිනම් User class එකේ no-arg constructor එකක් හදන්න

    PHP:
    public function __construct(){
    }
     
    Last edited:

    Ayeshlive

    Well-known member
  • Jul 1, 2011
    10,570
    943
    113
    *̶͑̾̾​̅ͫ͏̙̤g͛͆̾ͫ̑͆&
    1. Oyage getUsers method eke di oya aluthin config instance ekak hadanawa. Harinam oya use karanna one $this->config mokada oya try karanne class ekata object karaka thiyena db connection eka use karanna nisa.

    2. Constructor eka ta config object ekak pass karanna one.

    DooA kiyala thiyena de 100% hari.

    Anith eka machan getUsers hari onema getter eka ka di monawath print karanna epa. Array ekak return karanna. Presentation later eke di print karanna.
     
    Last edited:

    pen02x01

    Member
    Dec 9, 2015
    17
    0
    0
    public function __construct(config $config){
    }

    කෝඩ් එකේ හැටියට user class එකේ constructor එකට parameter එකක් pass කරන්න ඕනේ. උබ user instance එක හදන කොට pass කරලා na.

    PHP:
    //this config var need to pass into user ins
    $config = new Config();
    $userObj=new user($config); 
    
    
                $userResults=$userObj->getUsers(); 
                foreach ($userResults as $specificUser){ 
                    echo $specificUser['name']; 
                    echo '</br>'; 
                }

    එහම නැතිනම් User class එකේ no-arg constructor එකක් හදන්න

    PHP:
    public function __construct(){
    }

    machan oya widi dekatama karala baluwa eth wada nahane:sorry:config eka pass kala oya widiyata new object ekak hadala menna me error eka enawa,

    Fatal error: Class 'config' not found in C:\xampp\htdocs\php_app\Admin\user.php on line 473
     
    Last edited:

    pen02x01

    Member
    Dec 9, 2015
    17
    0
    0
    1. Oyage getUsers method eke di oya aluthin config instance ekak hadanawa. Harinam oya use karanna one $this->config mokada oya try karanne class ekata object karaka thiyena db connection eka use karanna nisa.

    2. Constructor eka ta config object ekak pass karanna one.

    DooA kiyala thiyena de 100% hari.

    Anith eka machan getUsers hari onema getter eka ka di monawath print karanna epa. Array ekak return karanna. Presentation later eke di print karanna.

    ela machan :D owu mama print karanne naha machan view ekedi thamai print karanne:yes:$this->config eka try karala balannan machan.uda daala thiyana anith widi try kala eth wada naha:confused:
     
    Last edited:

    pen02x01

    Member
    Dec 9, 2015
    17
    0
    0
    wade goda daagaththa anthimata:) Namespace daala kalama wade hari:D method names conflict ekak thibbe.comment karapu hamotama thanks:)
     
    Last edited: