Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, 20 July 2016

Passing JSON into php function as a parameter

Here is an example of hot to pass JSON into a php function as a parameter:

<?php

function print_details($json) {
 $args = json_decode($json);
 
 echo "My first name is {$args->first_name}<br />"; 
 echo "My last name is {$args->last_name}<br />"; 
 echo "My age is {$args->age}<br />";  
}


print_details('{
  "first_name": "keith",
  "last_name": "hards",
  "age": 37
}');
 

Friday, 16 October 2015

How to change composers install path


If you need to install composer modules to a custom install path other than vendors, then add the following to your config substituting XXXX/XXXXX to the custom install path that you require.

"config": {
        "vendor-dir": "XXXX/XXXXX"

},

Thursday, 10 September 2015

Zend Server Optimizer+ cache problems

If Zend Optimizer is caching your files and new changes are not reflected when the code is running, in PHP you can call:

accelerator_reset()

This will clear the Optimizer cache.
http://files.zend.com/help/Zend-Server-5/zend_optimizer+_-_php_api.htm

Debugging PHP constants

Handy tip:
If you'r bodded down in old legacy PHP code and you need to know what constants are defined at any point during execution, simply call:

get_defined_constants(true)

You can either add this to Zend Debugger as an expression or error_log with print_r it.

Tuesday, 29 July 2014

PHP onject to array

Handy bit of PHP to convert and object to an array 
 
public function objectToArray($obj) 
{
    if(!is_array($obj) && !is_object($obj)) 
    return $obj;

    if(is_object($obj)) 
    $obj = get_object_vars($obj);

    return array_map(array($this, 'objectToArray'), $obj);
}

Monday, 24 February 2014

PHP Array to stdClass <–> stdClass to Array

Function to Convert stdClass Objects to Multidimensional Arrays

<?php
 
 function objectToArray($d) {
  if (is_object($d)) {
   // Gets the properties of the given object
   // with get_object_vars function
   $d = get_object_vars($d);
  }
 
  if (is_array($d)) {
   /*
   * Return array converted to object
   * Using __FUNCTION__ (Magic constant)
   * for recursive call
   */
   return array_map(__FUNCTION__, $d);
  }
  else {
   // Return array
   return $d;
  }
 }
 
?>

Function to Convert Multidimensional Arrays to stdClass Objects

<?php
 
 function arrayToObject($d) {
  if (is_array($d)) {
   /*
   * Return array converted to object
   * Using __FUNCTION__ (Magic constant)
   * for recursive call
   */
   return (object) array_map(__FUNCTION__, $d);
  }
  else {
   // Return object
   return $d;
  }
 }
 
?>

Function usage

 // Create new stdClass Object
 $init = new stdClass;
 
 // Add some test data
 $init->foo = "Test data";
 $init->bar = new stdClass;
 $init->bar->baaz = "Testing";
 $init->bar->fooz = new stdClass;
 $init->bar->fooz->baz = "Testing again";
 $init->foox = "Just test";
 
 // Convert array to object and then object back to array
 $array = objectToArray($init);
 $object = arrayToObject($array);
 
 // Print objects and array
 print_r($init);
 echo "\n";
 print_r($array);
 echo "\n";
 print_r($object);
Test output:
stdClass Object
(
    [foo] => Test data
    [bar] => stdClass Object
        (
            [baaz] => Testing
            [fooz] => stdClass Object
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

Array
(
    [foo] => Test data
    [bar] => Array
        (
            [baaz] => Testing
            [fooz] => Array
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

stdClass Object
(
    [foo] => Test data
    [bar] => stdClass Object
        (
            [baaz] => Testing
            [fooz] => stdClass Object
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

Thursday, 12 April 2012

Some reasons for not using PHP

An interesting rand about PHP's deficiencies.
"PHP is not merely awkward to use, or ill-suited for what I want, or suboptimal, or against my religion. I can tell you all manner of good things about languages I avoid, and all manner of bad things about languages I enjoy. Go on, ask! It makes for interesting conversation.
PHP is the lone exception. Virtually every feature in PHP is broken somehow. The language, the framework, the ecosystem, are all just bad. And I can’t even point out any single damning thing, because the damage is so systemic. Every time I try to compile a list of PHP gripes, I get stuck in this depth-first search discovering more and more appalling trivia. (Hence, fractal.)"
http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/