undefined variable in construct function [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I think this might be a simple issue of variable scope, but I'm stumped as to where the issue lies. Given the following lines of code
class mysqlaccess {
private $creds;
private $error;
protected $con;
public $dir;
public function __construct () {
$this->$dir = "../../../../../private/mysqlinfo.ini";
}
}
when I try to reference this public variable from another file like so
include_once ('mysqlaccess.php');
$s = new mysqlaccess();
echo $s->dir;
I get the following errors
undefined variable dir
and
cannot access empty property
my understanding was that this was how the construct function was supposed to work. Am I missing something?

Typo here -
$this->$dir = "..
^
should be
$this->dir = "..

You need: $this->dir instead of $this->$dir.

Look here as example.
You have to use this(without $):
$this->dir

Related

php using class based on string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
you suppose we have a simple string as class name and namespace, for example:
$className = "post";
$nameSpace = "\\App\\Models\\";
$class = $nameSpace.Str::studly($className);
my question is how can i use this variables to use Laravel, methods such as find, for example:
//$data = Post::find(1);
$data = $class()::find(1);
here we can make a new stance from this string but i want to use Model function features.
i get this error:
Error
Call to undefined function App\Models\Post()
You need to do the following:
$class = "\\App\\Models\\Post";
$class = new $class;
$data = $class->find(1);
Or, as Tim said:
$class = "\\App\\Models\\Post";
$data = $class::find(1);

Php 7.2 class property tha contians array of objects [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm using php 7.2.
I need to instantiate an object of a class with a property that have to contain an array of objects.
When I try to add elements to array, I get an "Array to string conversion" error.
this is the code example:
class MyProduct
{
public $id;
public $stocks = array();
}
class MyStock
{
public $quantity;
public $leadTime;
}
private function Test()
{
$prod = new \MyProduct();
$prod->id = 1;
$stock1 = new \MyStock();
$stock1->quantity = 10;
$stock1->leadTime = 2;
array_push($prod->$stocks, $stock1);
}
How can I make it?
thanks in advance
Try array_push($prod->stocks, $stock1);
With $stocks you’re trying to refer to a variable called stocks.

Fatal error: Can't use function return value in write context in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm getting this error and I can't make head or tail of it.
The exact error message is:
function kdrusha_theme_create_page() {
require_once(get_template_directory().= '/inc/pages/kdrusha-settings.php');
}
add_menu_page("KD Rusha Options", 'KD Rusha', 'manage_options', 'kdrusha-options', 'kdrusha_theme_create_page','',99);
The problem is that you're using .=.
something .= something_else
is shorthand for
something = something . something_else
But your something is a function call, and it generally doesn't make sense to assign to a function call (the exception is when it returns a reference).
You should just use ., which concatenates its parameters and returns the result without assigning it anywhere.
require_once(get_template_directory() . '/inc/pages/kdrusha-settings.php');
You need to put your function return in some variable:
function kdrusha_theme_create_page() {
$template = get_template_directory();
require_once($template.'/inc/pages/kdrusha-settings.php');
}

Object of class could not be converted while assigning to variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following PHP code:
private $settings;
private function __construct($settings) {
$this->$settings = $settings;
print "Created compiler";
}
Where the received $settings is an associative array loaded from a JSON file, the thing is a keep getting this error (Note is implementing a singleton pattern):
Catchable fatal error: Object of class stdClass could not be converted to string
I'm sure this is a silly question, but I'm completely stuck at this moment...
The variable is defined as
private $settings;
Now inside the constructor or within the same class you can access the member variable as
$this->variable_name ;
Note that you do not need to have a $ before the variable name.
So in your case you should do as
$this->settings ;

PHP method to add to key-value array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having to put together a PHP snippet involving capturing form submission data. I'm trying to do this the "right" way with OOP PHP, and I'm struggling with array usage since my Googling seems to bring up sample code that is either too simple or too complex for what I'm trying to do.
How do I create a class method that adds keys and values to an array?
My code, which does not work currently:
class Connection
{
private $postItem = array();
public function addPostItem($key,$value)
{
$postItem[$key] -> $value;
}
public function printPostItem() {
return $this->postItem;
}
}
$c1 = new Connection;
$c1->addPostItem('FirstName','John');
$c1->addPostItem('LastName','Doe');
$c1->addPostItem('Email','JohnDoe#mail.com');
var_dump($c1->printPostItem()); // shows no array content
If I'm going against other best practices here, please let me know.
Use this:
public function addPostItem($key,$value)
{
$this->postItem[$key] = $value;
}
Change:
$postItem[$key] -> $value;
To:
$this->postItem[$key] = $value;
Also as Amal said turn on error reporting.

Categories