Php 7.2 class property tha contians array of objects [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 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.

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 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);

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.

undefined variable in construct function [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 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

Categories