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;
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 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
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 7 years ago.
Improve this question
Hello i try to implent a Language Changer in my Script.
But in my Class Function it is saying:
Notice: Undefined variable: lang in ......
I use a if else at begin of my Index.php which decide to includes the correct lang_xxx.php.
And in my lang_xx.php there is Array
/*
-----------------
Language: English
-----------------
*/
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
And echo in my Class then
<?php echo $lang['welcome_msg']; ?>
So my Question why isnt this working and what is the correct way to implent this?
Its also not working when i include the lang_xx.php File directly in the Class Function.
Use 'global' to declare that the variable exists outside of the class.
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
class Foo {
function bar(){
global $lang; // declare $lang as global, inside the method
echo $lang[]...
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I created a class with a constructor (__construct()), but I don't want anyone to be able to access it. How can I do that? Thank you very much!
Edit 1:
For more detail: I created a class:
<?php
class test{
function __construct()
{
$a=1;
}
}
$t = new test;
$t->//here's the problem
?>
In my editor, when press $t->, the code hint shows the ('_construct()') and ('$a') too.
I want to ask: Can someone else can acces ('$a') or ('_construct()').
How can I prevent that,
Just make the constructor private
class Test {
private function __construct() {}
}
If you don't let any access the constructor function for your class - no one will be able to use that class as they will not be able to instantiate it.
In any case, if they have your class file they will be able to look at the source code and see the constructor.