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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 months ago.
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.
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.
Improve this question
I am trying to access an associative array $foo from a function inside the class. When I log the contents from another function it is empty. I am really unsure what I am doing wrong.
class Item {
function __construct($x = 1) {
$y = do_something($x);
$foo = [
'id' => $y['anotherID'],
'name' => $y['name']
];
}
function insertData($data) {
$variable = $this->foo['id'];
// if I print $this->foo['id'] I get no output
}
}
I have also tried another recommendation of using self::$foo but got all sorts of errors about private and static?
You need to use $this->foo = [] instead of $foo = []. It makes it a property of the class.
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.
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 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts
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');
}
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