How To Create Dynamic Folder In Php With Example
Create PHP Classes with Dynamic Functions
As you have probably found out, when I see something interesting I don't know how to do, my only goal is to figure out how to do it. Thus is the case with PHP classes featuring dynamic methods based on database records (or arrays). I took some time to figure out how it was done.
The Sample MySQL Record in a PHP Array
$record = array( 'id' => 12, 'title' => 'Greatest Hits', 'description' => 'The greatest hits from the best band in the world!' );
Pretend we created the above array based on a record retrieved via a MySQL query.
The PHP Class with Dynamic Functions
/* create class */ class Record { /* record information will be held in here */ private $info; /* constructor */ function Record($record_array) { $this->info = $record_array; } /* dynamic function server */ function __call($method,$arguments) { $meth = $this->from_camel_case(substr($method,3,strlen($method)-3)); return array_key_exists($meth,$this->info) ? $this->info[$meth] : false; } /* uncamelcaser: via http://www.paulferrett.com/2009/php-camel-case-functions/ */ function from_camel_case($str) { $str[0] = strtolower($str[0]); $func = create_function('$c', 'return "_" . strtolower($c[1]);'); return preg_replace_callback('/([A-Z])/', $func, $str); } } The first step is to create a very primitive class with a constructor which receives the record array and a __call magic method which gets executed any time a class method is called. When a method is called we parse the method name to remove "get" and check to see if the corresponding array key exists. If the key exists, we return its value -- if the key does not exist, we simple return false.
The PHP Example Usage
/* usage */ $Record = new Record( array( 'id' => 12, 'title' => 'Greatest Hits', 'description' => 'The greatest hits from the best band in the world!' ) ); /* proof it works! */ echo 'The ID is: '.$Record->getId(); // returns 12 echo 'The Title is: '.$Record->getTitle(); // returns "Greatest Hits" echo 'The Description is: '.$Record->getDescription(); //returns "The greatest hits from the best band in the world!"
Above we pass the array to the class and are then able to retrieve values via the "get" functions. Awesome!
The above example is not the most dynamic or realistic usage of dynamic function creation but this example will get you crawling before walking. Let me know if you have better examples or real uses.
Recent Features
-
Conquering Impostor Syndrome
Two years ago I documented my struggles with Imposter Syndrome and the response was immense. I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions. I've even caught myself reading the post...
-
Incredible Demos
-
Create an Animated Sliding Button Using MooTools
Buttons (or links) are usually the elements on our sites that we want to draw a lot of attention to. Unfortunately many times they end up looking the most boring. You don't have to let that happen though! I recently found a...
-
Image Reflection with jQuery and MooTools
One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...
How To Create Dynamic Folder In Php With Example
Source: https://davidwalsh.name/dynamic-functions
Posted by: pantonemenim69.blogspot.com

0 Response to "How To Create Dynamic Folder In Php With Example"
Post a Comment