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);
}
Related
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);
Here is my php code, the test method not giving wanted output, and the other weird thing is var_dump('a') print 3 times;
my wanted output is array('qtggccc','qtff23sdf');
public function main()
{
$serverIds = array('ff23sdf','ggccc');
$res = $this->test($serverIds);
var_dump($res);
}
public function test($serverIds,$imgArray = array())
{
if(count($serverIds) > 0){
$media_id = array_pop($serverIds);
$imgUrl= $this->hh($media_id);
array_push($imgArray,$imgUrl);
var_dump($serverIds);
var_dump($imgArray);
$this->test($serverIds,$imgArray);
}
var_dump('a');
return $imgArray;
}
public function hh($m)
{
return 'qt'.$m;
}
Try this:
class MyClass{
private $imgArray = array();
public function main(){
$serverIds = array('ff23sdf','ggccc');
$res = $this->test($serverIds);
print_r($this->imgArray);
}
public function test($serverIds){
if(count($serverIds) > 0){
$media_id = end($serverIds);
$imgUrl= $this->hh($media_id);
array_push($this->imgArray,$imgUrl);
//remove last element
array_pop($serverIds);
$this->test($serverIds);
}
return;
}
public function hh($m){
return 'qt'.$m;
}
}
$obj = new MyClass();
echo '<pre>';
$obj->main();
Why use recursion? You are using a complicated solution for a simple problem.
public function main()
{
$serverIds = array('ff23sdf','ggccc');
$res = array();
//These three lines replace an entire recursive function, making the code easier and saving a chunk of memory once you start using real arrays
foreach ($serverIds as $media_id){
array_unshift($res, $this->hh($media_id));
}
var_dump($res);
}
public function hh($m)
{
return 'qt'.$m;
}
So I got this example layout.
private $_getMilk() = '';
public function getMilk():string {
return $this->_milk;
}
public function setMilk(string $milk) {
$this->_milk = $milk;
}
SetMilk is also used to empty milk which sounds weird to me why set empty string if you ask for milk.
Should I instead also create the function emptyMilk. (asume the milk property is getting called alot)
public function emptyMilk() {
$this->_milk = '';
}
A benefit of a seperate emptyMilk() function is that it allows you to use a special representation for an empty object, rather than exposing that to the callers.
private $is_empty = true;
public function getMilk(): string {
if ($this->$is_empty) {
throw new Exception("Empty milk");
}
return $this->$_milk;
}
public function setMilk(string $milk) {
$this->is_empty = false;
$this->_milk = $milk;
}
public function emptyMilk() {
$this->is_empty = true;
$this->_milk = null;
}
public function gotMilk(): boolean {
return !$this->is_empty;
}
This allows you to use any value for $_milk rather than making one value special.
Is it possible to use public functions inside a public function in php?
I got a few public functions which change the input and return it. I want to make a for statement inside a public function that loops through my functions, like:
$input
for= function1 -> output1 -> function2->output2->function3->output3.
I want to use the output of that for my next function. Also the 4 functions I have in my for loop has to loop 9 times.
in this case its about AES encrypt. i got 4 functions called: subBytes, shiftRows, mixColumns, addRoundkey.
This is my public function encrypt:
public function encrypt($input)
{
$functions= ('subBytes', 'shiftRows', 'mixColumns', 'addRoundKey' );
foreach($functions as $function)
{
$input = $$function($input);
}
return($input);
} //end function encrypt
and this is one of my functions:
public function subBytes($state)
{
for ($row=0; $row<4; $row++){ // for all 16 bytes in the (4x4-byte) State
for ($column=0; $column<4; $column++){ // for all 16 bytes in the (4x4-byte) State
$_SESSION['debug'] .= "state[$row][$column]=" . $state[$row][$column] ."-->" . self::$sBox[$state[$row][$column]]."\n";
$state[$row][$column] = self::$sBox[$state[$row][$column]];
}
}
return $state;
}
Use code like this:
$output3 = function3(function2(function1($input)));
Or you can to add your function name into array and iterate over it:
$input = ''; // some value
$functioins = ('function1', 'function2', 'function3', 'function4');
foreach ($functions as $function) {
$input = $$function($input);
}
$output = $input;
If we try to use public functions of object then:
public function encrypt($input)
{
// array with methods names
$methods= array('subBytes', 'shiftRows', 'mixColumns', 'addRoundKey' );
foreach($methods as $method)
{
$input = $this->$method($input);
}
return($input);
}
This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 8 years ago.
I have this Deprecated warning after switching my php to 5.5.8,
Deprecated: preg_replace(): The /e modifier is deprecated, use
preg_replace_callback instead in C:\wamp\www...Curly.php on
line 28
This is the function in my class of Curly,
public function replace ($input, $options = array()) {
return preg_replace("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
}
so if I use preg_replace_callback instead,
return preg_replace_callback("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
Then I get this error,
Warning: preg_replace_callback(): Requires argument 2,
'$this->_replace("\1",$options)', to be a valid callback
in C:\wamp\www...Curly.php on line 28
Any ideas how can I fix this?
EDIT:
class Curly extends CoreModel
{
// Set the property/ variable of this class
public $constant = null;
/**
* Extend the parent class property.
*/
public function __construct($connection){
// Extend parent's.
parent::__construct($connection);
$this->constant = new Constant($connection);
}
/**
* Replace the curly in an input string
* #param string $input
* #return string
*/
public function replace ($input, $options = array()) {
//return preg_replace("/\{{2}([a-z]+\|.+)\}{2}/Ue",'$this->_replace("\\1")',$input);
//return preg_replace("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
return preg_replace_callback(
"/\{\{([a-z_]+(?:\|.+)?)\}\}/U",
function($m) { return $this->_replace($m[1], $options); },
$input
);
}
/**
* Run the replacement code on a given macro string
* #param string $input
* #return string
*/
private function _replace ($input,$options) {
// Set local vars.
$defaults = array();
// Call internal method to process the array.
$array = parent::arrayMergeValues($defaults,$options);
//print_r($array);
// Convert array to object.
$property = parent::arrayToObject($array);
// type-checking comparison operator is necessary.
if (strpos($input, '|') !== false) {
//VERTICAL SIGN FOUND
list ($name,$params) = explode("|",$input);
if (method_exists($this,$name)) {
return $this->$name($params);
}
throw new Exception ("Unrecognised macro: {$name}.",500);
} else {
// Get the input string and request the data from constant table.
$value = $this->constant->getRow($input)->value;
// If there is a value returned from the contstant table.
if($value !== null) {
// Return the what is returned from the the constant table.
return $value;
} else if(isset($property->$input)) { // If there is a customised value from the developer.
// Return what is customised by the developer.
return $property->$input;
} else { // Nothing is found.
// Return what is from the input.
return "{{{$input}}}";
}
}
}
/**
* Replaces a YouTube curly
* #param string $params
* #return string
*/
private function youtube ($params) {
parse_str($params);
// set defaults
if (!isset($id)) { $id = "ykwqXuMPsoc"; }
if (!isset($width)) { $width = 560; }
if (!isset($height)) { $height = 315; }
// output the final HTML
return "<iframe width=\"{$width}\" height=\"{$height}\" src=\"http://www.youtube.com/embed/{$id}\" frameborder=\"0\" allowfullscreen></iframe>";
}
}
How about:
public function replace ($input, $options = array()) {
return preg_replace_callback(
"/\{\{([a-z_]+(?:\|.+)?)\}\}/U",
function($m) use($options) { return $this->_replace($m[1], $options); },
$input
);
}
Otherwise with call_user_func_array:
return preg_replace_callback(
// RegExpr
"/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",
// Callback
array($this, '_replace'),
// Data
$input
);