object of class cannot be converted to string - php

I know this is an old question but I am not finding answer. Here is my code:
class SqlConnect implements connectionInterface {
//put your code here
private $datasource=null;
private $credentials = null;
public function __construct() {
$netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();
$credentials = $this->$netsConn->getBasicCredentials();
}
public function connect()
{
$conn = mysqli_connect($datasource['servername'], $credentials['username'], $credentials['password']);
if (!$conn)
{
die("Connection failed: " . $conn->connect_error);
} else {
return $conn;
}
}
}
I am then calling the class like
class applicationsController implements Controller {
private $conn = null;
public function __construct() {
$conn = new SqlConnect();
$this->$conn->connect();
}
...
}
However I am getting the following error:
Catchable fatal error: Object of class configInit could not be converted to string
This error happens in SqlConnect. I have looked through about 6 answers on stackoverflow and most all of them were about trying to echo some objects, which is not my case

Ahh, I think I see it.
You are assigning to global variable $netsConn, not object property $netsConn ...
Try this:
private $netsConn;
...
$this->netsConn = new configInit();
... that is, if you want to keep it around. Otherwise, refer directly to the variable not to a (never-declared ...) property.

The reason you get the error is because of the following 2 lines
netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();
First line is fine, but on the second line, you are using $netsConn as an attribute name for the SqlConnect class ($this). For this to work, php has to use the string representation of $netsConn by attempting to call it's implementation of the magic method __toString(). Since this probably does not exist in your configInit() class, you end up with that error.

Related

Uncaught Error: Using $this

I've seen other posts about this but I just can't understand them, what is the solution here, can you guys show me? Please. Static or not static? What does it mean? Is this the problem?
Db connection code:-
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'not telling');
class DB_con {
public $connection;
function __construct(){
$this->connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD,DB_DATABASE);
if ($this->connection->connect_error)
die('Database error -> ' . $this->connection->connect_error);
}
function ret_obj(){
return $this->connection;
}
}
Code for fetching records:-
<?php
$role_id = (isset($_SESSION['role_id']) ? $_SESSION['role_id'] : 4) ;
$query = "
SELECT rights_codename FROM permissions
INNER JOIN roles_and_permissions ON fk_permissions_id = permissions_id
WHERE fk_role_id = $role_id
";
$_SESSION['permissions'] = array();
$result = $this->db->query($query);
while($row = $result->fetch_assoc()){
array_push($_SESSION['permissions'], $row['permissions_cname']);
// $_SESSION['permissions'][] = $row['permissions_cname'];
}
if(in_array('admin_rediger_bruger',$_SESSION['permissions'])){
}
?>
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\R_L_E\login.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\R_L_E\login.php on line 30
In lay man terms, $this is when you are referencing a non static function in a class. What you can do is create a new instance of the database class and then use that variable created during instantiation to access the member functions of that class
Do this
$conn = new DB_con();//instantiate the class.add this at the very top of login.php
$conn->connection->query('your sql stuff');//replace the $this->db->query($query) with this line
You can access the connection property as it is declared as public in your class
Also do not forget to include the DB_con file
$this is a variable that refers to "the current object" when you're running code inside that object. That means, when you write this:
class AClass {
public function foo() {
var_dump($this);
}
}
$my_object = new AClass;
$my_object->foo();
Then $this is the same as $my_object during the execution of the foo() function. You can think of it like an extra parameter to the function, once you leave that function it won't exist any more.
So when you write this:
$result = $this->db->query($query);
You need to be inside some method which has been called on a particular object, so that there is an object for $this to refer to. If you're in global code, or a function that's not part of a class, or a static method, there is no "current object", so $this doesn't exist.
In your case, you're trying to get to some instance of a DB connection object. That means somewhere in your code you need to have created that object, and assigned it to a variable, then you can reference that variable. You can call that variable whatever you like, but you can't call it $this, because that's a reserved name.
Your DB_Con class doesn't have a query() method, so it looks like you want to get the MySQLi object out first, and then call the method on that.
So you would write something like:
$my_db_connection = new DB_Con;
$mysqli_connection = $my_db_connection->connection;
// or: $mysqli_connection = $my_db_connection->ret_obj();
$result = $mysqli_connection->query($query);
Or more concisely:
$my_db_connection = new DB_Con;
$result = $my_db_connection->connection->query($query);
// or: $result = $my_db_connection->ret_obj()->query($query);

PDO database connection can not be used in other classes, but others can? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I've been working on a databaseHandler php class, which should connect to the database and then be usable in other PHP files, atleast this was the plan. I've come across the problem that it cannot use any PDO related functions in my PHP class, i've tried checking if it was null, or not set at all (but it was) and i've also tried using a dummy function that just echos something which was to test if the class isn't undefined in others.
<?php
class databaseHandler {
private $db;
private static $instance;
function __construct() {
$this->buildDatabaseConnection();
}
public static function getInstance() {
if(!self::$instance) {
self::$instance = new databaseHandler();
}
return self::$instance;
}
private function buildDatabaseConnection() {
require 'dbconfig.php';
try {
$this->db = new PDO("mysql:host=" . HOST . ";dbname=" . DATABASE . ";charset=utf8", USER, PASSWORD);
if(isset($this->db)) {
if(!is_null($this->db)) {
echo "isn't null";
}
}
} catch(PDOException $e) {
$e->getMessage();
}
}
public function getConnection() {
return $this->$db;
}
public function getSomeShit() {
echo "some shit";
}
}
?>
The problem might be with the getConnection() php function, does it actually return the PDO object here? Because the main problem lays with the fact that i get this error when i use my getConnection function in other classes:
Notice: Undefined variable: database in F:\Websites\DevProject\php\functions.php on line 69
Fatal error: Uncaught Error: Call to a member function prepare() on null in F:\Websites\DevProject\php\functions.php:69 Stack trace: #0 F:\Websites\DevProject\php\functions.php(51): register('qwe', 'sdasd', 'asdasd', 'asdas', '01-01-1970') #1 F:\Websites\DevProject\register.php(123): datelessRegister('qwe', 'sdasd', 'asdasd', 'asdas') #2 {main} thrown in F:\Websites\DevProject\php\functions.php on line 69
and line 69 would be this:
$stmnt = $database->prepare("SELECT $username, $email FROM " . USERTABLE);
Where the $database variable is the class instance:
$database = databaseHandler::getInstance();
If i try to var_dump it in my registration php file, it'll tell me this:
object(databaseHandler)#1 (1) { ["db":"databaseHandler":private]=> object(PDO)#2 (0) { } }
However, when i use it inside of my function, it will say it is 'NULL', why is it null in a function but defined globally?
All the errors relate to the object, and in there it tells me the pdo is undefined (i've tried checking if it was null, but it wasn't!)
Any help would be great, thanks in advance.
In the return statement of getConnection method you should write the class variable without $:
return $this->db;
Also, you must do the queries on the PDO connection instead of your database handler class, so $database = databaseHandler::getInstance(); should be
$database = databaseHandler::getInstance()->getConnection();
or you can simply use the __call magic method to keep your code as it is right now:
public function __call($method, $args)
{
return call_user_func_array(array($this->db, $method), $args);
}
Also, to clarify how to use this class, you should call it inside every method you need the database to be used, not only once on the top of your file, because defining it there it will not be available inside your methods/functions unless you don't use the global statement to import the variable. However usage of the global variable is not encouraged and it is considered as a bad practice. An example on how to use the database would be:
public function doSomething()
{
$db = databaseHandler::getInstance()->getConnection();
$db->prepare(....);
}
public function doSomethingElse()
{
$db = databaseHandler::getInstance()->getConnection();
$db->prepare(....);
}
If you know there are many methods inside your class that uses the database then you can define it as a class variable and set the db instance in the constructor.
You seem to be having issues with scopes.
$database = databaseHandler::getInstance();
function doSomething()
{
// $database is not defined in this scope
$database->getConnection()->prepare(/* ... */);
}
You have to declare it as part of the global scope
function doSomething()
{
global $database;
$database->getConnection()->prepare(/* ... */);
}
The problem seems to be in the getConnection() function. Since $db is field you must do.
return $this->db;
However this is a very complex class structure for a simple problem.
You can directly extend your class from PDO
class DatabaseHandler extends PDO {
function __construct() {
parent::__construct('mysql:host=host_name;dbname=db_name', 'user_name', 'password');
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
And create an object of this whenever you want to do database transaction.
$db = new DatabaseHandler();
$stmt = $db->prepare(".....");
$stmt->execute();

Use MySQLi object based within a class

require("classes/controller.class.php");
$db = new mysqli(__DBHOST, __DBUSER, __DBPASS, __DBDATA);
if ($db->connect_errno) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
$controller = new Controller;
Within the Controller class I want to use the $db MySQLi connection. So within a public function I used:
$result = $db->query("SELECT * FROM colors");
var_dump($result);
But I get this error:
Fatal error: Call to a member function query() on a non-object in /home/cloud/public/teamdresser/controllers/IndexController.php on line 8
Am I doing something wrong? Do I need to pass the $db connect first, or use global or something?
Thanks in advance
If $db is a global variable, you'll need to import it in your function:
public function YourFunction()
{
global $db;
$result = $db->query("SELECT * FROM colors");
..
}
Instead of doing this, you might want to consider encapsulating $db in a database class and create it as a singleton, or, arguably better, pass it to the constructor of Controller. But that remark might lead to new discussions about design, while complete books have been written about this subject. So for now, just import the global like described above. ;)
You could use a class with a singleton instance
class Database {
private static $_instance;
public static function getInstance() {
//If the connection is not initialized, this creates it and store
if (empty(Database::$_instance))
Database::$_instance = new Database();
return Database::$_instance;
}
private $_db;
private function __construct() {
$this->_db = new mysqli(__DBHOST, __DBUSER, __DBPASS, __DBDATA);
if ($db->connect_errno) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
}
private function __destruct() {
$this->_db->close();
}
public function query($query) {
return $this->_db->query($query);
}
}
You called in this manner
$result = Database::getInstance()->query("SELECT * FROM colors");
You have created a connection and executed with calling a function. Once the script is terminated, the connection will be autorealeased by the destructor.
This should be only a start point. You could create all functionality that you want (e.g. escape parameters, insert, update, delete, and so on...)

What's wrong with PDO in other class?

I'm just changing my website's MySQL to PDO, and I've got a strange problem when I tried to use PDO in an other class.
class Database {
private $pdo;
public function __construct() {
$this->pdo = new PDO('mysql:host=localhost;dbname=appdora;charset=utf8', 'root', 'root');
}
}
class doClass {
//Variables
private $db;
//PDO
public function __construct(Database $db) {
$this->db = $db;
}
And the code is returns with: the following error:
Catchable fatal error: Argument 1 passed to doClass::__construct() must be an instance of Database, none given, called in .../index.php on line xx and defined in ../classes.php on line xx
The code:
$do = new doClass();
if ($do->loginCheck()) { echo 'loginOk'; } else { 'loginError'; }
loginCheck() is a simle function that works without classes!
Could you help me, what's the problem?
Thanks in advance!
$do = new doClass();
You defined your doClass class to expect a parameter in the constructor:
public function __construct(Database $db)
So you need to supply that parameter of type Database to successfully construct the object.
For example, if you have a database object stored before somewhere inside a variable $database, you can simply pass it to the constructor of doClass like this:
$do = new doClass($database);

MySQLi as an static class

I have the following code that is able to create a class that has a single static instance of the database object, and two static functions for rows and columns.
<?php class Database{
private static $instance;
private function __construct() {}
private function __clone(){}
public static function call(){
if(!isset(self::$instance)){
self::$instance = new MySQLi("localhost", "foo", "bar", "fizz");
if(self::$instance->connect_error){
throw new Exception('Error MySQL: ' . self::$instance->connect_error);
}
}
return self::$instance;
}
public static function getRow($id, $db){
$query = "SELECT * FROM ".$db." WHERE id=".$id;
$result = self::call()->query($query);
return $result->fetch_assoc();
}
} ?>
However, when I call getRow from another class, like this
$data = Database::getRow($id, "posts");
I get the following error:
Fatal error: Call to a member function
fetch_assoc() on a non-object in
database.php
on line 27
And I check it again and again and everything seems to be in order, maybe I have a error in call() ?
That will happen if you have an error in your SQL syntax -- did you check it and make sure there were no errors?
I would have error checking, something like
if( self::$instance->error ) {
// Handle error
}
You are passing in a variable called $db to getRow(), maybe that should be a table?
query() will return false if the SQL query fails. You can add the following to handle this error:
$result = self::call()->query($query)
if($result === false)
throw new Exception(call()->error); // throw exception, use MySQLi error as message
return $result->fetch_assoc();
public static function getRow($id,$table)
{
$query = "SELECT * FROM ".$table." WHERE id=".$id." LIMIT 1";
$result = $this->instance->query($query);
return $result->fetch_assoc();
}
this line
if(!isset(self::$instance)){
will have sometimes strange behaviours. checking isset() against a null value is subject to failings. try this:
private static $instance = null; // it defaults to null but hey, rather be on the sure side
// foo
if (self::$instance === null) {
// mysqli creation foo
I have tried this code and it works, you must have an error in the query. What may cause this error is:
Check what you pass to Database::getRow(). There may be some non-numerical characters in your id
Check if column name 'id' exists, this sounds funny but it happened to me a million times, i was querying column 'id' instead of 'product_id' and so on
Check if table $db exists
Make sure you don't switch the function parameters by an accident. Database::getRow(24, 'people'); instead of Database::getRow('people', 24);
I can't think of anything else that may cause this error.

Categories