Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Talk!
PHP OOP issue
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="pen02x01" data-source="post: 19411320" data-attributes="member: 555538"><p><span style="font-size: 12px">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,</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">[PHP]<?php</span></p><p><span style="font-size: 12px">require_once ("db.php");;</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">class user{</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> public $name;</span></p><p><span style="font-size: 12px"> public $age;</span></p><p><span style="font-size: 12px"> public $contact;</span></p><p><span style="font-size: 12px"> public $config;</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> public function __construct(config $config){</span></p><p><span style="font-size: 12px"> $this->config=$config;</span></p><p><span style="font-size: 12px"> }</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> public function setUser($name,$age,$contact){</span></p><p><span style="font-size: 12px"> $this->name=$name;</span></p><p><span style="font-size: 12px"> $this->age=$age;</span></p><p><span style="font-size: 12px"> $this->contact=$contact;</span></p><p><span style="font-size: 12px"> }</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> public function getUsers(){</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // We need to have 1 config object with all data in.</span></p><p><span style="font-size: 12px"> $config = new config("localhost", "root", "", "app_db", "", "mysqli"); //type in your data here…</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // Now we need to have access to the db class, we uses the config object to configure the db object.</span></p><p><span style="font-size: 12px"> $db = new db($config);</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // We can now open the connection to the database.</span></p><p><span style="font-size: 12px"> $db->openConnection();</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // If your config details are right, we are now connected to a database, lets test the connection before we run queries.</span></p><p><span style="font-size: 12px"> $are_we_online = $db->pingServer();</span></p><p><span style="font-size: 12px"> // The variable $are_we_online should be true (or 1) if we are connected to the server.</span></p><p><span style="font-size: 12px"> //echo "Are we online: " . $are_we_online; // prints 0 or 1.</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // Let us run a query.</span></p><p><span style="font-size: 12px"> $sql = $db->query("SELECT * FROM {users}");</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // The variable $sql will now hold the data returned from the database, we can now work with it.</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // Does it have rows ?</span></p><p><span style="font-size: 12px"> /*$hasRows = $db->hasRows($sql);</span></p><p><span style="font-size: 12px"> echo "Does it have rows: " . $hasRows; // prints 0 or 1 (true or false).</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // How many rows does it have.</span></p><p><span style="font-size: 12px"> $countRows = $db->countRows($sql);</span></p><p><span style="font-size: 12px"> echo "How many rows: " . $countRows; // returns the number of rows.*/</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // We can get the data from the fetch_assoc function.</span></p><p><span style="font-size: 12px"> //$result = $db->fetchAssoc($sql);</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> // We can get the data from the fetch_array function.</span></p><p><span style="font-size: 12px"> $result = $db->fetchArray($sql);</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> //We can even print out the latest used query:</span></p><p><span style="font-size: 12px"> //echo $db->lastQuery();</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> return $result;</span></p><p><span style="font-size: 12px"> }</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px">}[/PHP]</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">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,</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">[PHP]</span></p><p><span style="font-size: 12px">require_once("classes/users.php");</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> $userObj=new user();</span></p><p><span style="font-size: 12px"> $userResults=$userObj->getUsers();</span></p><p><span style="font-size: 12px"> foreach ($userResults as $specificUser){</span></p><p><span style="font-size: 12px"> echo $specificUser['name'];</span></p><p><span style="font-size: 12px"> echo '</br>';</span></p><p><span style="font-size: 12px"> }</span></p><p><span style="font-size: 12px">[/PHP]</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">menna me error eka print wenawa php page eke,</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">"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"</span></p></blockquote><p></p>
[QUOTE="pen02x01, post: 19411320, member: 555538"] [SIZE="3"]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; } }[/PHP] 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>'; } [/PHP] 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"[/SIZE] [/QUOTE]
Insert quotes…
Verification
Haya warak paha keeyada? (haya wadi kireema paha)
Post reply
Top
Bottom