As you know, getClass() has been deprecated in PHP 8. So I replaced it with ->getType()->getName() (another option is ::Class) but after this change, VS Code warned for newInstance()
Call to a member function newInstance() on a non-object of type stringPHP(PHP0404)
is this an error or it is OK?
the full code is as follow:
$body = json_decode(file_get_contents('php://input'), true);
$mClass = $methodParams[count($methodParams) - 1]->getType()->getName();//::Class;//getClass();
$mdl = $mClass->newInstance($body);
Related
I have this piece of code:
private function _resolveCustomEntries($fields)
{
foreach ($fields as &$value) {
if (array_key_exists('custom', $value)) {
if (method_exists($this, $value['custom'])) {
$value['custom'] = $this->$value['custom'](); //variableInterpolation
}
}
}
return $fields;
}
I ran a PHP 7.2 compatibility check and it complained here with the "variableInterpolation" on the marked line. When I run this code, the PHP log tells me this:
ERR (3): Notice: Array to string conversion in
/public_html/lib/KiTT/Payment/Widget.php on line 217
Thats the same line where the "variableInterpolation" check failed. So how would I rewrite this code so it works in PHP 7.2?
Thanks!
Solution:
$value['custom'] = $this->$value['custom']();
has to look like this:
$value['custom'] = $this->{$value['custom']}();
It's a matter of order variables are evaled.
With
class x {
public function y() {
echo 'ok';
}
}
$x = new x();
$y = array('i' => 'y');
Then
$x->$y['i']();
Fails because PHP first tries to cast the $y variable into a string, and get the matching property of $x (which btw does not exist), then tries to get the index 'i' or that unexisting property, and then tries to run it as a callable.
Hence 3 errors:
Array to string conversion
Undefined property x::$Array
Function name must be a string (nda: the undefined property returns NULL)
Instead, curly brace the variable to set the resolving order:
$x->$y['i']();
Will work. So use $this->{$value['custom']}()
This will throw an array to string conversion in 7.2
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$value['custom'] = $this->$value['custom']();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
But it will execute just fine in 5.6.
Then i changed the snippet to this, not calling the method directly casting the array key to function name, but initializing a string (hopefully, there is no type validation in your code) variable with the function name first:
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$functionName = $value['custom'];
$value['custom'] = $this->$functionName();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
This will run just fine in php 7.2
You could try rewriting your code using complex (curly) syntax, you can read more about it here.
Your code would look something like this.
$value['custom'] = $this->{$value['custom']}();
Edit: moved the curly braces to correct positions.
I have a class which I am using $this with and since upgrading from 5.6 to 7.1 I cannot get to work. I'm confused as to why? Please see code sample below:
class user_BL extends BLL {
public function getCurrentUserFromSession($userid) {
$userrecord = array();
$query = new query();
$query->addCriteria("userid", $userid , "=");
$userrecords = $this->getDataByQueryObj($query, new user_DAL());
if (isset($userrecords[0])){
$userrecord = $userrecords[0];
$lastlogindetails = $this->getLastLoginDetailsAsArray();
$userrecord['logindatetime'] = astlogindetails['logindatetime'];
}
return $userrecord;
}
}
getLastLoginDetailsAsArray is a function on the BLL base class. The IDE interprets this ok and sees that its available to be used.
Anyhelp with this would be really helpful.
Thanks,
Deano
EDIT: the error I'm getting is Fatal error: Uncaught Error: Using $this when not in object context
Forgot to add that it was a long day yesterday. :)
After digging into the error I've found the call was being made like this:
user_BL::getCurrentUserFromSession();
which seems to be the problem. Calling it like this:
$userbl = new user_BL();
$userbl->getCurrentUserFromSession();
Corrects this.
Thanks for everyones help.
Trying to crate objects dynamically for a plug in system (work in progress)
heres my code using $this->module->load_module('test'); to use the method that creates the dynamic objects. Following code is the function that loads the class's and makes use of an auto loader, i have checked that its getting the correct file etc.
<?php
class BaseModule {
function __construct() {
}
function load_module($module){
echo 'Module = '.$module.'<br />';
$object_name = $module . "Controller";
$this->$$module = new $object_name();
}
}
Here is a test module that it would load when invoking $this->module->load_module('test'); and it creates the object outputting the test strings via echo statements. Heres the code for the test module that was constructed. Which should cause no problems as there is not really a solution but just an output string, but posted any way.
<?php
class testController {
function __construct() {
echo 'test controller from modules <br />';
}
}
However when running the page i am getting some errors can any one help out?
Notice: Undefined variable: test in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
Fatal error: Cannot access empty property in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
I have a PHP class that is used to generate some HTML in the following way:
public $rName;
public $cName;
public $rMonth;
function __construct(){
$this->report = new DOMDocument;
$this->report->loadHTMLFile('template.php');
}
private function addComponent($tag, $content){
$parent = $this->report->getElementById('content');
$child = $this->report->createElement($tag, $content);
$parent->appendChild($child);
}
function addSection($header){
$this->addComponent('h2', $header);
}
function addSubHeader($subHeader){
$this->addComponent('h3', $subHeader);
}
function addContent($content){
$this->addComponent('p', $content);
}
which is being called like this:
$report = new Report;
$outputType = $_GET['outputType'];
$report->rName = 'rName';
$report->cName = $_GET['cName'];
$report->rMonth = $_GET['rMonth'];
$report->addSection('Section');
$report->addSubHeader('SubHeader');
$report->addContent('Content');
Using XAMPP on Windows, this code works absolutely fine. However on a centos environment I get the error:
Call to a member function appendChild() on a non-object on line 16
Line 16 is:
$parent->appendChild($child);
The template.php file appears to be loading, and there is a div with the id of "content", however a gettype() on $parent shows it as NULL.
Pretty stumped at the moment. Any ideas?
In older versions of PHP, getElementById requires that an id attribute has been specified in a DTD.
Inserting <!DOCTYPE html> at the start of the HTML may be enough, in this case.
I am trying to create a function that changes the locale of a site based on the domain extension but when I try and set the value of a Variable in AppModel as a function I get an error. I am not sure what I am doing wrong.
FYI: $_SERVER['HTTP_HOST'] = '.de';
class AppModel extends Model {
//var $locale = 'de_de'; // Example of what I need
var $locale = $this->getLocale();
function getLocale() {
$domain = explode('.', $_SERVER['HTTP_HOST']);
if ($domain[1] == 'de') {
return 'de_de';
} else {
return 'en_gb';
}
}
}
Error Returned:
Parse error: syntax error, unexpected T_VARIABLE in /var/www/devsite/v1/site/app/app_model.php on line 7 Call Stack: 0.0002 671648 1. {main}()
/var/www/devsite/v1/site/app/webroot/index.php:0 0.0255 5883776 2. Dispatcher->dispatch() /var/www/devsite/v1/site/app/webroot/index.php:83 0.0264 5949592 3.
Dispatcher->__getController() /var/www/devsite/v1/site/cake/dispatcher.php:116 0.0264 5949672 4. Dispatcher->__loadController()
/var/www/devsite/v1/site/cake/dispatcher.php:385 0.0265 5951760 5. App->import() /var/www/devsite/v1/site/cake/dispatcher.php:413 0.0265 5953552 6.
App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0265 5954000 7. App->import()
/var/www/devsite/v1/site/cake/libs/configure.php:1171 0.0265 5957624 8. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0268 5984264 9.
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019 0.0269 6047416 10. require('/var/www/devsite/v1/site/app/app_controller.php')
/var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0269 6047560 11. App->import() /var/www/devsite/v1/site/app/app_controller.php:8 0.0270 6051456 12.
App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0270 6052240 13. App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1036
0.0272 6164128 14. require('/var/www/devsite/v1/site/cake/libs/sanitize.php') /var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0272 6164416 15.
App->import() /var/www/devsite/v1/site/cake/libs/sanitize.php:2 0.0273 6165128 16. App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0337 8579264 17.
App->import() /var/www/devsite/v1/site/cake/libs/configure.php:1149 0.0337 8582864 18. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0338 8583952 19.
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019
Thanks in advance.
the error message says it pretty clearly: invalid php
you still have to code valid PHP (even if it is cakePHP)! using a proper IDE will outline the error right away:
var $uses ('App');
should be
public $uses = array('App');
as documented!
if you are still on PHP4 you would need "var" instead of "public"
you should really start reading basic php books because you seem to lack the basic stuff.
you can also not use dynamic methods in the class declaration:
var $locale = $this->getLocale();
you need to use the constructor for this:
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->locale = $this->getLocale();
}