Dynamic Matrices in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I have written the PHP code below in order to create a dynamic matrix with different count of columns per row but PHPStorm says the row variable is not defined. please help.
class reply
{
public $text;
private $row = array(array());
private $rowIndex = 0;
private $colIndex = 0;
public function Add($menu)
{
$this->$row[$this->rowIndex][$this->colIndex] = $menu;
$this->colIndex++;
}
public function NextRow()
{
$this->rowIndex++;
$this->colIndex = 0;
}
}

$this->$row is incorrect, it should be $this->row.

Related

PHP - variable in constructor is empty in other functions of the class [closed]

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.

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.

Shorten String function not working properly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years 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 have this little function to stop a string being too long but it dosen't seem to work. I'm assuming i've done something wrong?
function trimString($string, $maxChar) {
$string = (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}
I was using it like this:
echo trimString($row['mainTitle'], 30);
Thanks
You forgot to return value from function. Try
function trimString($string, $maxChar) {
return (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}

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.

Strange call to a member function on a non-object php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
So I create a Supermarket class and initiate two objects like this:
$items[0] = new Supermarket("Item1", 2.70);
$items[1] = new Supermarket("Item2", 1.0);
Then I call the showItem() method on the first object of the array and it works:
$items[0]->showItem();
But when I try to use a for or a foreach loop through the array to show all the items I get the non-object error. The following won't work either:
$i = 0;
$items[i]->showItem();
Any ideas?
You are not using the variable sign $
$i = 0;
$items[$i]->showItem();
change like
$i = 0;
$items[i]->showItem();
$i = 0;
$items[$i]->showItem();
$items[$i] instead of $items[i]

Categories