Message: Array to string conversion only in PHP7 Codeigniter - php

I have added PHP7 to my local server and I got this error:
Message: Array to string conversion Filename:
libraries/Data_views_array.php Line Number: 59
data = $this->CI->$this_data[0]->$method($pass_var);
I found this post: PHP Notice: Array to string conversion only on PHP 7 and I changed my code to:
data = $this->CI->$this_data[0]->{$method($pass_var)};
but I got a different error:
Message: Call to undefined function get_page_id()
Everything works in PHP 5.6 and I don't know how to make it work in PHP 7.
Please help.
class Data_views_array {
var $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function action_per_module($array_modules) {
$array_to_display = array();
$array_view_data = array();
if (!$array_modules) {
return false;
}
else {
while (list($key, $this_data) = each($array_modules)) {
/*
* $this_data[0] - is the controller name
* loading this model
*/
$this->CI->load->library($this_data[0]);
if ($this_data[2] == 'NULL') {
/*
* if there are no arguments (method and passing variable)
* the model is called
*/
$data = $this->CI->$this_data[0];
}
else {
/*
* getting method and passing variable from arguments
*/
//echo $this_data[2]. " - json<br>";
$obj = json_decode($this_data[2]);
$method = key(get_object_vars($obj));
//echo $method. " - method<br>";
$pass_var = $obj->$method;
/*
* getting data for view
*/
$data = $this->CI->$this_data[0]->$method($pass_var);
}
/*
* name of the View
*/
$view = $this_data[1];
/*
* adding the pair of View to the $array_view_data
*/
array_push($array_view_data, $view);
array_push($array_view_data, $data);
/*
* passing View Array to Display array
*/
array_push($array_to_display, $array_view_data);
/*
* clear the array of the pair of View and passing variable
*/
unset($array_view_data);
$array_view_data = array();
}
}
return $array_to_display;
}
}
The first Class that is called - $this_data[0]== page_title_display from library has method: get_page_id($page_id). This class gets the Title of the page.
Why the same script works in PHP 5.6 and it does not work in PHP 7?
Why the get_page_id function is found in PHP 5.6 but not in PHP 7?
class Page_title_display {
var $CI;
public function __construct(){
$this->CI =& get_instance();
}
public function get_page_id($page_id) {
$this->CI->load->model('pageTitle_model');
$title = $this->CI->pageTitle_model->getPagetitle($page_id);
return $title;
}
}

I solved this issue. For some reason PHP 7 does not like when $this_data[0] is used as a part of array.:
data = $this->CI->$this_data[0]->$method($pass_var);
First solution: I assigned this value to a variable:
$libControl = $this_data[0];
data = $this->CI->$libControl->$method($pass_var);
Second solution: only $this_data[0] in brackets:
data = $this->CI->{$this_data[0]}->$method($pass_var);

Related

ERROR on PHP Notice: Array to string conversion in hash function

I want to build blockchain using PHP-OOP.
my code:
class Block {
public function __construct($timestamp, $transactions, $previousHash = null) {
$this->previousHash = $previousHash;
$this->timestamp = $timestamp;
$this->transactions = $transactions;
$this->nonce = 0;
$this->hash = $this->calculateHash();
$this->difficulty = 2;
}
/** Returns the SHA256 of this block (by processing all the data stored inside this block)*/
public function calculateHash() {
return hash("sha256", $this->previousHash.$this->timestamp.((string)$this->transactions).$this->nonce);
}
}
It shows me this error:
PHP Notice: Array to string conversion in /home/istabraq/bctest/test2/a2.php on line 14
any idea please?
That transform array to string you can use implode function:
public function calculateHash() {
return hash("sha256", $this->previousHash.$this->timestamp.(implode('', $this->transactions)).$this->nonce);
}

Notice: Undefined variable: cur_order in

I am getting an error : Notice: Undefined variable: cur_order in ,can anyone guide me where i am went wrong,below is my code:
<?php
class Products {
var $name;
var $description;
var $productId;
function Products($id = 0, $infoArr = array()) {
if ($id > 0 && count($infoArr)) {
$this->name = $infoArr['name'];
$this->description = $infoArr['description'];
$this->productId = $id;
}
}
/**
* #static
*/
function loadAllProducts() {
$arr = getProducts();
$prods = array();
foreach ($arr as $id=>$info) {
$prods[$id] = new Products($id,$info);
}
return $prods;
}
/**
* function loadOrderDetails
*
* This function should be giving us all the information about the order:
* The customer's name and address, the products that were ordered (deescriptions too) and the order totals.
* See products that php to see what is expected to be shown
*
* #param Integer $order_id the unique identifier for the order
* #return Array $cur_order the details of the order
*
*/
function loadOrderDetails($order_id) {
$orders = getOrderInfo();
$products = getProducts();
$customer = getCustomerInfo();
$address = getAddresses();
return $cur_order;
}
}
?>
$cur_order does not exist in your code. How can you return it?
function loadOrderDetails($order_id) {
$orders = getOrderInfo();
$products = getProducts();
$customer = getCustomerInfo();
$address = getAddresses();
return $cur_order; // <-- this variable does not exist
}
You can only return one of the four variables you have used in that method or any of the member variables of that class (although that wouldn't be relevant in this case).
Another observation, you pass $order_id as a parameter but never use it. Do you really need to have it be a parameter then?
In your function "loadOrderDetails" you are returning variable $cur_order. It doesn't look like you've defined this variable yet.
If you were to return $address it would probably work. Or within the function define $cur_order = "test"
You are returning the $cur_order variable inside the loadOrderDetails function but you have not declared it in the function.
Just use $cur_order = true;
If you need to load the variable from outside the function use global $cur_order;

PHP 5 function to PHP 4

I must convert a web application from php 5 to php 4, but I'm having some problems, especially with objects.
I'm getting a error in a setter function with array arg (parse error, unexpected '=', expecting '(' on line 20).
The code:
class Fecha extends DateTime {
var $dias = array();
function DateTime($fechaHora = 'now') {
parent::__construct($fechaHora, new DateTimeZone('Europe/Madrid'));
}
/*
* Getters / Setters
*/
function setDias($dias) {
if (count($dias) == 7)
self::$dias = $dias; // Here is where the error is thrown
}
}
and I call the class like that:
Fecha::setDias(array('Luns', 'Martes', 'Mércores', 'Xoves', 'Venres', 'Sábado', 'Domingo'));
Please provide more code..
According this you should use this:
static $dias = array();
function DateTime($fechaHora = 'now') {
parent::__construct($fechaHora, new DateTimeZone('Europe/Madrid'));
}
/*
* Getters / Setters
*/
function setDias($dias) {
if (count($dias) == 7)
self::$dias = $dias; // Here is where the error is thrown
}

notice error on arrayObject

Hello i'm having trouble with this class when it comes to multidimensional arrays and setting them and i cannot understand why is behaving like this...
Anyone who can help me fix it?
Few Usage Examples:
echo $config->session->gcDivisor;
echo $config['session']['gcDivisor'];
$config['logged'] = '1';
$config->logged = '1';
$config['user']['logged'] = '1';
$config->user->logged = '1';
Errors for each of the lines
Notice: Trying to get property of non-object in /home/biroexpert/subdomains/prod/index.php on line 33
200
Notice: Undefined index: user in /home/biroexpert/subdomains/prod/Library/Twelve/Config/Config.php on line 67
Notice: Indirect modification of overloaded element of Twelve\Config\Config has no effect in /home/biroexpert/subdomains/prod/index.php on line 37
Notice: Undefined index: user in /home//subdomains/prod/Library/Twelve/Config/Config.php on line 87
Notice: Indirect modification of overloaded property Twelve\Config\Config::$user has no effect in /home//subdomains/prod/index.php on line 38
Strict Standards: Creating default object from empty value in /home//subdomains/prod/index.php on line 38
Code
<?php
namespace Twelve\Config;
use \ArrayObject;
use \Twelve\SingletonPattern\LazySingleton;
use \Twelve\SingletonPattern\Interfaces\ILazySingleton;
/**
* Singleton With Configuration Info
*/
class Config extends ArrayObject implements ILazySingleton
{
/**
* Stores FileName
* #var Config
*/
protected static $_configFile = '';
/**
* Config Settings Array
* #var Config
*/
protected $_settings = array();
public static function getInstance(){
return LazySingleton::getInstance(__CLASS__);
}
/**
* Set the Config File Path
*/
public static function setFile($filePath)
{
static::$_configFile = $filePath;
}
public function __construct()
{
LazySingleton::validate(__CLASS__);
$values = include(static::$_configFile);
if(is_array($values))
{
// Allow accessing properties as either array keys or object properties:
$this->_settings = new ArrayObject($values, ArrayObject::ARRAY_AS_PROPS);
}
}
// some debug method
public function dumpArray()
{
echo "<pre>";
print_r($this->_settings);
}
public function getIterator()
{
return $this->_settings->getIterator();
}
public function offsetExists($index)
{
return $this->_settings->offsetExists($index);
}
public function offsetGet($index)
{
return $this->_settings->offsetGet($index);
}
public function offsetSet($key, $value)
{
return $this->_settings->offsetSet($key, $value);
}
public function offsetUnset($index)
{
return $this->_settings->offsetUnset($index);
}
public function count()
{
return $this->_settings->count();
}
/**
* Prevent Cloning
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
// No Cloning Allowed
}
}

Cannot access function within model in ZEND

Can anyone see why my check_multi function would return a --
Fatal error: Call to undefined function check_multi() in
/var/www/vhosts/aero.onelinksoftware.com/application/models/Design.php
on line 21
The above error shows up and I am not sure what I am doing wrong. I tried setting my function as public, private, static, and other combinations but no matter what I try; the system still errors out. Can you not call a function inside a Model in Zend? I don't understand why I cannot use a function I created if it is inside a class I made.
If I echo and die before the check_multi call; I can see my text and such. I have also performed a php test for syntax and it is valid as far as it reports.
class Model_Design
{
/**
* Constructs our partials.
*
* #return void
*/
public function __construct($key)
{
// Get the DB Connection
$db = Zend_Registry::Get('db');
// Setup the SQL Statement
$sql = $db->select()->from('design', array('id'));
// Get the Result
$result = $sql->query();
// Get our Row
$row = $result->fetchAll();
if(check_multi($key, $row)) {
echo "omg"; die();
}
// Make sure the id isn't empty
if (empty($key)) {
throw new Exception('You have a disturbing lack of variables.');
}
// Store the id
$this->variables = $key;
// Construct our query
$sql = $db->select()->from('design')->where('`id` = ?', $key);
// Get the result
//$result = $sql->query();
//$row = $result->fetch();
}
private function check_multi($n, $arr)
{
foreach ($arr as $key => $val) {
if ($n===$key) {
return $key;
}
}
return false;
}
}
Try:
$this->check_multi($key, $row);
To access a variable or function from inside its container class you must use $this. $this is the current instance of the class.
How do you call the function ? use $this->check_multi($n,$arr); or you can try function_exists() to check if function really exist

Categories