New object with name in variable - php

I am trying to initiate an object with a dynamic path. I have the variable $model with the model name.
$model = "foo";
$class = new \Path\To\$model();
I get the error
Parse error: syntax error, unexpected '$model' (T_VARIABLE), expecting identifier (T_STRING)
If I try $class = new \Path\To\{$model}();
I get the error
Parse error: syntax error, unexpected '{', expecting identifier (T_STRING)
When I try
namespace \App\Models
$class = new $model();
I get the error Class 'foo' not found
When I try $class = new \Path\To\foo(); it works.
Any ideas?

Try:
$class = "\Path\To\foo";
$object = new $class();
Or:
use Path\To\foo;
$class = foo::class;
$object = new $class();

You can store path in variable:
$path = "\Path\To\\";
and then generate class name like this:
$className = $path.$model;
$class = new $className();

Related

syntax error, unexpected token "<<", expecting "function" or "const"

I read this article after upgrading my local dev environment to php8.
And implement it in my class, like:
<<Attribute('foo')>>
public function index()
{
$posts = (new Post)->get()->withMany('image');
return $this->app()->view('index', compact('posts'));
}
and it return error.
syntax error, unexpected token "<<", expecting "function" or "const"
What is really a proper of doing that in php8. Am i missing something?
Correct attribute syntax in PHP 8 is:
#[Attribute('foo')]
https://www.php.net/releases/8.0/en.php
public function create(Request $request)//create object of the request
{
$student = new Student;//create object of the model
$student->name = $request->name;
$student->city = $request->city;
$student->marks = $request->marks;
$student->save();
return redirect('index');//if we write this out of the curly braces then this error show
}

how to get through this Fatal Error problem?

**Fatal error: Method Template::__toString() must not throw an exception, caught ParseError: syntax error, unexpected end of file in C:\xampp\htdocs\joblister\index.php on line 0**
i dont know where is the wrong part? can you guys help me? i kept trying but nothing can fix it.
<?php include_once 'config/init.php'; ?>
<?php
$job = new Job;
$template = new Template('templates/frontpage.php');
$template->title = 'Latest Jobs';
$template->jobs = $job->getAllJobs();
echo $template;
?>
this is because you are trying to echo an object not string ...
in the template class you either need to implement the magic function __toString() and define how the object should be printed or you may have your custom method in the class for example getHtml() .. which returns the built string and echo it like that
echo $template->getHtml();

PHP Kohana parse simple XML

Having some problem parsing some XML in an application I am building. The format is as follows
<taskResponse statusCode="200">
<session-states>
<min-state>dfgdgdgd</min-state>
<max-state>dgdfgd</max-state>
<session-info>dgffdgd</session-info>
<project-id>B19DEDCC11D4E0EFC000EB9495D0F44F</project-id>
</session-states>
</taskResponse>
Attempting to use the following code to parse out the session-state value
public function parse_session_state($xml){
echo $xml;
$xml = new SimpleXMLElement($xml);
echo $xml->session-states[0]->min-state;
die();
}
but I get an error message
ErrorException [ Parse Error ]: syntax error, unexpected '[', expecting ',' or ';'
This seems to work:
public function parse_session_state($xml){
$xml = new SimpleXMLElement($xml);
echo $xml->{'session-states'}->{'min-state'};
die();
}

PHP how to have declare a class member?

Slightly confused with PHP because it does not declare object variable types.
This is simplified code which does not work. I get why I get an error but not sure how in PHP I can specify that $pb is a PushBot object and so it has methods which can be used.
class Bot
{
public $pb;
//Constructor
function __construct(){
require_once('../PushBots.class.php');
// Application ID
$appID = '';
// Application Secret
$appSecret = '';
// Push The notification with parameters
$this ->pb = new PushBots();
$this ->pb->App($appID, $appSecret);
}
//Method. The $this->pb->Push() does not work
public function sendMessage(){
$this->pb->Push();
}
}
//Client calling the class
$bot = new Bot();
$bot->sendMessage();
The error I get is :
Parse error: syntax error, unexpected '$' for when the line
$this->pb->Push();
is called.
I guess its because it does not know that $pb is a PushBot object at this stage ?
Can I not declare it something like :
Public Pushbot $pb;
Parse error: syntax error, unexpected '$' for when the line
This is a parse error, meaning your code has not even been run yet. Check your code for any sytnax errors (the code you posted does not contain the syntax error).
how in PHP I can specify that $pb is a PushBot object
Although unrelated to the syntax error, if you were using some sort of dependency inversion you could use type-hinting to require a certain object to be passed:
// will cause fatal error if anything other than an object of type Pushbot is passed
function __construct(Pushbot $pb)

Several MySQL users in Joomla 1.5 configuration.php

In order to solve MySQL 1226 error i follow this recomendation, but it doesn't work.
I'm getting an error:
Parse error: syntax error, unexpected T_VARIABLE in configuration.php on line 38
configuration.php content:
1:<?php
2:class JConfig {
.....
37:public $users = array("root", "boot", "foot");
38:public $user = $users[array_rand($users)];
.....
90:}
91:?>
Please help me fix it.
Problem is solved by adding __construct() function:
var $user = '';
var $users = array("root", "boot", "foot");
public function __construct() {
$this->user = $this->users[mt_rand(0, count($this->users) - 1)];
}

Categories