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.
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 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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm coming into object-oriented PHP from a more functional Javascript background, so I'm fairly new to both PHP and OOP.
What I'm trying to do is make a class that that is passed the contents of a text file, divides it up into manageable pieces, and then loops over those pieces (steps), and handles them accordingly, sometimes making HTTP requests, sometimes saving chunks of info into a $fileVariables array. Here's a basic outline of what I have:
class Script {
public $file, $client, $fileVariables;
function __construct($file) {
$this->file = $file;
$this->client = new \GuzzleHttp\Client();
$this->fileVariables = array();
}
public function parseAndRun() {
$client = $this->client;
$file = $this->file;
// this is the function that performs the bulk of the class's
// work. It relies on some utility functions that are defined
// after, and are utilized within this function like this:
$steps = $this->divideFileIntoSteps($file);
// or like this:
$this->setVariable($index, $value);
}
public function divideFileIntoSteps($file) {
// return array of file divided into steps
}
public function setVariable($index, $value) {
// push $index => $value into $fileVariables array
}
}
I think it's possible that I'm trying to do something that would be totally okay in functional Javascript, but just feels sloppy in object-oriented PHP. Also, when I run PHPUnit tests on this, I get an error that's something like Serialization of 'Closure' is not allowed, which I think from doing some googling on the error might have something to do with how I'm doing this. Also, the fact that I have to use $this-> over and over again on commonly used functions makes me feel like I'm doing something wrong. It just gives me that bad code feeling.
Is there a better way to do this, bearing in mind that a lot of the functionality has to do with getting and setting items in the $fileVariables array?
Your code is basically correct.
You don't need $file = $this->file;
$this prefix is standard for accessing member functions and variables in PHP.
Your function could look like this:
public function parseAndRun() {
$steps = $this->divideFileIntoSteps($this->file);
// or like this:
$this->setVariable($index, $value);
}
Another best practice is to declare private all methods that you don'n need to access from outside the class. Member variables should all be private and you should have accessor functions for them.
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;
This question already has an answer here:
Accessing variables through classes
(1 answer)
Closed 9 years ago.
I have the following class:
class validationHandler{
private $dataType; //set via constructor ...
private $validation = null;
private function requireValidation(){
if($this->validation == null){
$this->validation = loadDataFromJSONfile($this->dataType);
}
}
public function validate($data){
$this->requireValidation();
//validate... the rules are in the $validation-variable
}
}
When an object needs to validate some data, it makes a new validation-object and calls validate().
The first time something needs to be validated, the data is loaded from a json-file.
But: Sometimes I have several Objects which need the same validation-file. And This code loads the file for each Object.
Question: Is there a way to set $validation global, so that every object accesses the same variable, but without loosing the private-property?
(Notice that I have different validation-files and different kinds of objects. An objects tells the validationHanlder in __construct() which type it has, and which validation-file should be loaded. So I need $validation to be an array. (Didn't write it in the code, so it's more readable)
You want to create your variable using static, it sounds like.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
When declaring an array as a class member, which way should it be done?
class Test1 {
private $paths = array();
public function __construct() {
// some code here
}
}
or
class Test2 {
private $paths;
public function __construct() {
$this->paths = array();
// some code here
}
}
Which one is better in terms of good practices and performance? What would you recommend?
I'd suggest doing this when declaring a class variable. A constructor can be overriden in extending classes, which might result in E_NOTICEs or even E_WARNINGs if any of your functions depend on this variable being an array (even an empty one)
If you are going to populate your array dynamically during initialization, do it in the constructor. If it contains fixed values, do it in the property declaration.
Trying to populate an array dynamically (e.g. by using the return value of a certain function or method) within the declaration results in a parse error:
// Function call is not valid here
private $paths = get_paths();
Performance is not a real concern here as each has its own use case.
In general, because I write mostly in other languages besides PHP, I like to declare my instance variables outside of the constructor. This let's me look at the top of a class and get an idea for all properties and their access modifiers without having to read the code.
For example, I really don't like methods like this
// ...
// whole bunch of code
// ...
public function initialize() {
$this->foo = array();
// some other code to add some stuff to foo
}
Now, if I just look at the class, I can't be sure there is a variable foo even available. If there is, I don't know if I have access to it from anywhere outside the instance.
If instead I have:
public $foo = array();
at the top of my class, I know that foo is an instance property, and that I can access it from elsewhere.
There are no performance implications. Stop obsessing over things that don't matter - concentrate on performance problems that ARE there: measure first, optimize only the top offenders.