$mt = microtime(TRUE);
foreach($y as $k => $v) {$z->$k = $v;}
$a = microtime(TRUE) - $mt;
print "Execution Time: " . ($a) . "\n";
azurepopcorn
Optimize Life
Monday, May 25, 2020
Friday, February 14, 2020
Are PHP Objects passed as a value?
Yes they are, but as a copy of a value of a reference. Same as in Java.
Example below is showing how the Person type arguments are passed. In the point of entry of the methods changeName() and changeNameToNull(), $person reference copy is made.
From that point, a new reference can change the $person object in changeName(), as it has a reference to it. On the other hand, changeNameToNull() shows it is a copy of a reference, as setting to NULL value just destroys the copied reference, but not the $person object, thus original reference also.
<?php class Person { public $name = 'Mike'; } class Change { public function changeName(Person $person): void { $person->name = 'Jack'; } public function setPersonToNull(Person $person): void { $person = null; } } //set person to NULL $person1 = new Person(); (new Change())->setPersonToNull($person1); var_dump($person1); //change name to Jack $person2 = new Person(); (new Change())->changeName($person2); var_dump($person2);
Result:
object(Person)#1 (1) {
["name"]=>
string(4) "Mike"
}
object(Person)#2 (1) {
["name"]=>
string(4) "Jack"
}
Wednesday, August 22, 2018
PHP Foreach Key by Pointer
Here is a way to add a new elements to a new array in PHP foreach function, by reference on a new element.
$items = Item::get(); $formattedItems = []; foreach ($items as $item) { $fm = &$formattedItems[]; $fm['id'] = $item->id; $fm['label'] = $item->label; $fm['icon'] = $item->icon; } var_dump($formattedItems);
Simple and clean.
Get Service Instance Anywhere in Laravel
Inject Service Instance by Constructor
Code Example:
Code Example:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyController extends Controller
{
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function store(Request $request)
{
$response = $this->client->message()->send([
'text' => $request->input('text'),
]);
}
}
Tuesday, June 5, 2018
PHP - remove all except letters, numbers, dashes and blanks
PHP - remove all except letters, numbers, dashes and blanks for various cases, like cleaning user input for DB storage, further execution and making it safe.
It's very easy to remove special characters.
$search = preg_replace('/[^a-zA-Z0-9 -]/', '', $search);
Also, if you would like to check if search string contains some characters, it's like this:
if (preg_match('/[^a-zA-Z0-9 -]/', $search)) {
echo 'invalid';
} else {
echo 'valid';
}
Or in opposite case, to chech only for special characters:
preg_match('/[$&+,:;=?@#|\<>^*()%!]/', $userInput)
Labels:
clean input,
PHP,
regex,
remove special characters.
Wednesday, February 28, 2018
Image Meta Data&EXIF Removal
There are a lot of tools claiming to clean meta data from image, but this one called JPEG & PNG Stripper works just awesome! It provides super easy and efficient meta removal.
Need bulk image cleaning? It's a piece of cake with this software. Just select your images and drag&drop in the application window. Plus, you'll get old school software feeling, back in a days when software did it's purpose, nothing more or less.
JPEG & PNG Stripper link
Friday, December 7, 2012
Lee Scratch Perry movie - Vision of paradise
Well, this isn't Lee's movie, it's about the Lee Scratch Perry! Volker Schaner, a German guy is making the movie about Lee's life for last 13 years! Right now the project came to the final phase, where it should be done some final editing which costs minimum $40.000 $20.000.
The movie can be seen as a guide for how to change the world with music – with a positive attitude, mindset or as Lee Perry calls it: “vibration”.The interview on this project
I you like Lee's work , you can support this project. If you wish to donate, you can do it on this lee perry movie link.
If you don't know about Lee Perry, it's time to find him out. As he is a living legend, a great MAN, a great musician and a world's peace-fighter, Lee is surely human race avant-garde in every and each way.
update :
There is a new kickstarter page for this project: Lee Scratch Perrys - Vision of paradise
Facebook page: leescratchperrys.visionofparadise
Official website : visionofparadise.de
If you don't know about Lee Perry, it's time to find him out. As he is a living legend, a great MAN, a great musician and a world's peace-fighter, Lee is surely human race avant-garde in every and each way.
update :
There is a new kickstarter page for this project: Lee Scratch Perrys - Vision of paradise
Facebook page: leescratchperrys.visionofparadise
Official website : visionofparadise.de
Subscribe to:
Posts (Atom)