Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have a class called Users.php .Inside of this class i create a local variable like this:
class Users
{
public $dbHelper;
}
inside of the Users class is a function called init inside of this function i set the $dbHelper variable to be a object of the DatabaseHelper.php class i wrote:
public function init()
{
$this->dbHelper = new DatabaseHelper();
}
now i try to call a method inside of the DatabaseHelper class like this:
public function login()
{
$this->dbHelper->executeSQLCommandWR(// HERE COMES THE QUERY STRING //);
}
But currently its giving me this error:
Call to a member function executeSQLCommandWR() on null.
Why is the dbHelper variable null? In other oop programming langauages this works why not in this case?
Thanks for any help
Edit:
Im sorry i couldnt post all the code for this question because i was kind of in a rush when i created this question.
I call the init method as soon as my page gets loaded. It really seems like a scope problem i will try some of the answers and try to solve this problem. Thanks for everyone trying to help
I think you do not called the init, but init is not a good practice if you call it right after the instantiate. You can put it into the constructor, but that wont be a clean code too.
Instead of this use:
public function login() {
if (empty($this->dbHelper)) {
$this->dbHelper = new DatabaseHelper();
}
$this->dbHelper->executeSQLCommandWR(// HERE COMES THE QUERY STRING //);
}
or
public function login() {
$this->init();
$this->dbHelper->executeSQLCommandWR(// HERE COMES THE QUERY STRING //);
}
public function init() {
if (empty($this->dbHelper)) {
$this->dbHelper = new DatabaseHelper();
}
}
Second is better because of single responsible principle IMHO.
Notice that you have not initialized the member variable $dbHelper,
you haven't provided it with any value, therefore, it's null.
public function login()
{
$this->init();
$this->dbHelper->executeSQLCommandWR(// HERE COMES THE QUERY STRING //);
}
It seems to be a variable scope problem call the function init in the login function to define the variable
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I looked at the problems here on SO, and tried to fix the code accordingly, but I'm having a hard time understanding why this is not working.
I have a class in its namespace, and I'd like to use the methods from another class from another namespace in it. So I tried:
<?php
namespace Main_Namespace\My_Namespace;
use Other_Namespace\Cool_Class;
class Main_Class {
protected $cool_class;
public function __construct( Cool_Class $cool_class ) {
$this->cool_class = $cool_class;
}
public test_method() {
$some_boolean = $this->cool_class->some_method_that_returns_bool();
}
}
And I instantiate it elsewhere with new MainClass().
This fails, and tells me
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Main_Namespace\My_Namespace\Main_Class::__construct() must be an instance of Other_Namespace\Cool_Class, none given, called in...
I even tried with
use Other_Namespace\Cool_Class;
$cool_class_instance = new Cool_Class();
new Main_Class( $cool_class_instance );
Not working as well (I think I get class not found error).
Now, if I don't instantiate in the constructor, but inside a method like
<?php
namespace Main_Namespace\My_Namespace;
use Other_Namespace\Cool_Class;
class Main_Class {
public test_method() {
$cool_class = new Cool_Class();
$some_boolean = $cool_class->some_method_that_returns_bool();
}
}
This will work.
What am I missing here? What am I doing wrong?
For the first example:
$myObject = new MainClass();
The error you get is expected. You cannot call an empty constructor for a class that has a constructor with a typed parameter.
For the second example (calling a constructor with an initialized object as a parameter, which fails):
Edit: added details missing in the original quesion.
Please specify exact error message that you received - class load error
Please specify PHP version - 7.1
Please provide the custom autoloader - link in comment.
I suggest to debug the autoloader by dumping the values it is processing.
Example 3 is ok.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a simple API class that talks with its web-based counterpart.
All works fine, just one variable denies to be saved.
I have a variable called $scope, initialised at the top of the class:
class Api {
private $scope;
public function set_scope( $s ) {
$this->scope = serialize($s);
return true;
}
private function get_scope() {
return unserialize($this->scope);
}
}
Next I'm getting the scope via API from the web script as JSON, I json_decode it as an array (second parameter as true) and in that form, I pass it through set_scope() function.
I am 10000000% sure API returns JSON, when var_dump'ed it returns me a proper array data.
For some reason though, that data is not saved into the $scope variable.
Any ideas?
Edit #1:
I call function get_scope within the class, in another function. I was trying to set that variable directly, so just using $this->scope=$scope, without success though. I use the same structured functions to save/get other variables, and all of them are working, except this one.
Other function looks like that:
public function get_modules() {
$av_modules = $this->available_modules;
$scope = $this->get_scope();
var_dump($scope);
foreach($scope as $mod) {
$av_modules[$mod] = true;
}
return $av_modules;
}
The get_scope method is private (thus cannot be called from outside itself).
Make the get_scope method public and the value will be returned.
Your get_scope() function is declare as private. To use it outside class, you should change it to public.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to create a simple class in PHP, but i've got some trouble with a method call.
<?php
include('MySQL.php');
class User {
var $sql;
function _construct(){
// SQL connection
$this->sql = new MySQL(<<hidden>>, <<hidden>>, <<hidden>>);
}
public function login($username, $password){
// TODO
}
}
?>
At the //TODO section, i want to do a call like $this->sql->select('users'), but it won't let me do it. It gives an error and says that sql is a non-object.
Your "_construct" is missing a _ (you must have two), to it won't get called. Change it to :
public function __construct(){
It should work. Also remember to make it public.
If it's not called, you $sql variable isn't initialized and is actually a non-object for PHP.
Also, you may precise the visibility of your variable when declaring it, rather than using the deprecated var keyword :
private $sql;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
$Clint_ip=$this->request->clintIp();
May I get a clear concept about this line?In here I know $clint_ip is a variable,but what is the next three?which one is an object?
which one is a method?
which one is a class?
I just need to understand this line.In several project I have seen this types of line.In this line which one called object?If you want You can give another example.In here $this is an object?or class?or method?
Yes $Clint_ip is an variable,
Like other object oriented based programming languages $this is the this of a class consisting it. (For more about this When to use self over $this?)
request looks like an object of another class
and clintIp() is the public method of the class of the request object
The code you provided appears to be from inside of a class.
A class is denoted like this:
class Example {
private $foo;
public $bar;
public function __construct() {
}
public function method() {
}
private function other() {
}
}
When you create an object of this class, you can use the format:
$example = new Example();
This calls the constructor __construct().
Once you have created ("instantiated") this object, you can use the -> to call the properties of the object.
So, I can say
$example->bar = "Foo";
which sets this property to a string.
Your Code
In your code, the property "request" is itself an object (an instance of a class).
$Clint_ip=$this->request->clintIp();
Here is an example of the code this could be using
class Example {
public $request;
public function __construct($request) {
$this->request = $request;
}
}
class Request {
public function clintIp() {
//return something
}
}
And then some context:
$request = new Request;
$example = new Example($request);
$clint_ip = $example->request->clintIp();
So here, $clint_ip is the variable. $example and $request are objects (instances of classes), and clintIp() is a method of the request object.
Now, about "$this". This indicates that it is within the object "Example":
Imagine the class Example now has a method
public function test() {
return $this->request->clintIp();
}
$this means that it is inside of an instance of an object. In static context, use "self::", as mentioned in one of the other answers.
You are inside object which has request property. Request property contains object with method clintIp() which return client ip.
This question already has answers here:
Call to a member function on a non-object
(4 answers)
Closed 10 years ago.
I have recently started coding in php again and am learning Object Orientated PHP.
I have come across an issue that I cannot seem to get my head round with an error:
Fatal error: Call to a member function verify_Username_And_Password() on a non-object in C:\wamp\www\sso\lib\UserManagement.php on line 57
I understand that the program seems to think that the mysql class variable is empty but not why when it is instanced.
The function verify_Username_And_Password ($username, $password) is defined in the class Mysql.
The following is code from UserManagement.php with parts taken out that are not needed.
<?PHP
require_once 'lib/Mysql.php';
class UserManagement {
private $mysql;
function __construct(){
session_start();
$this->manage_Session();
$this->mysql = new Mysql();
}
function validate_User ($username, $password){
$user_Check = $this->mysql->verify_Username_And_Password($username, md5($password));
if($user_Check) {
return true;
} else {
return false;
}
}
}
I must be missing something basic about OO in PHP. I would be grateful for any advice anyone can give. I will be happy to post more code if needed.
As requested here is:
Mysql.php pastebin.
UserManagement.php pastebin
Without seeing the code that does the work, my guess would be you are using the validate_User method without first constructing a UserMangement object.
$um = new UserManagement();
$um->validate_User($username, $password);
If you wish to call validate_User without first constructing a UserManagement object it needs to be a static method. Without the UserManagement object being constructed Mysql is never constructed and saved in the attribute inside your class.