php using class based on string [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
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);

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.

Use of undefined constant i - assumed 'i' (this will throw an Error in a future version of PHP) (laravel post method error) [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 months ago.
Improve this question
I want to enter an multiple field entered data in table with for loop
but I am getting an error in the post method.
The error is:
Use of undefined constant i - assumed 'i' (this will throw an Error in a future version of PHP)
controller code:
$degree = $request->degree;
if($degree > 0)
{
for($i=0;$i<count($degree);$i++){
$edu = new education;
$edu->degree = $request->degree[i];
$edu->clg = $request->clg[i];
$edu->yoc = $request->yoc[i];
$edu->save();
}
}
so, please suggest me what I can do.
here there is a silly mistake bro you not remember to use $i inside loop for the degree, clg and yoc
$degree = $request->degree;
if($degree > 0)
{
for($i=0;$i<count($degree);$i++){
$edu = new education;
$edu->degree = $request->degree[$i];
$edu->clg = $request->clg[$i];
$edu->yoc = $request->yoc[$i];
$edu->save();
}
}

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

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