I'm trying to create an Autoloader class, so that I'll be able to load all modules automatically. But the problem is, I want to set a global from configuration file, and later, just call all of them by using:
Autoloader::GetGlobals();
So far, I have these 3 files:
Configuration.php
<?php
global $Configuration;
$Configuration['Globals'] = "Core Database Templates Directory Debugger";
?>
Autoloader.Class.php
<?php
require_once('Configuration.php');
private static $Globals = "";
private static $MakeGlobal = "global ";
public static function GetGlobals()
{
$ParsedGlobals = "";
$Globals2String = explode(" ", Autoloader::$Globals);
foreach($Globals2String as $Global)
$Globals[] = "$".$Global;
$DefinedGlobals = count($Globals);
for ($i = 0; $i < $DefinedGlobals; $i++)
{
$LastElement = $DefinedGlobals - 1;
if($i != $LastElement)
$ParsedGlobals .= $Globals[$i].", ";
else
$ParsedGlobals .= $Globals[$i].";";
}
return Autoloader::$MakeGlobal.$ParsedGlobals;
}
?>
I'm getting the right output:
global Core, Database, Templates, Directory, Debugger;
The next thing is that I want to interpret this as PHP code and not as a string, and I don't want to use eval() (because I've read many articles that says that this is the last function to be used).
So the question is, is it possible to run this string from return as PHP code by simply calling it as Autoloader::GetGlobals();?
It's almost as bad as using eval(), but there's variable variables, if you choose to go down this path of madness and chaos:
function foo($arg) {
global $$arg; // note the $$
echo "$arg / $$arg";
}
$a = 'bar';
foo('a');
output:
a / bar
Related
I've been working with Php for less than 2 months (this is also my first question so please tell me if I'm missing something) and it has been going smooth right up till today. I'm working on a form plugin for Wordpress and currently implementing the code to make the forms saved in the database to connect with a shortcode which includes the ID of the form in the database. The 1st form has an ID of 1 and the shortcode is IForm_1. Pretty simple.
The problem occurs when looping thru all the forms and not being able to pass the $ID value from the loop to the IForm function.
$ID= 0;
$FormID= 0;
settype($ID, "integer");
for ($x = 1; $x <= 300; $x++) {
global $ID;
$ID++;
$ShortCode = "IForm_";
$ShortCode .= $ID;
$FormID = $ID;
add_shortcode( $ShortCode, 'IForm_Array' );
$ShortCode ='';
}
here is the loop which is very simple, when the $ShortCode lines up with the shortcode used on the site it works and the IForm get used as it should.
function IForm(){
global $ID;
//testing_decode();
// Gets the value of baseTest from getDB and puts it in test.
$DBForm = getDB($ID);
$Form = $DBForm;
$Form .= "Works but not really";
return $Form;
}
Here is the function.
Problem is that $ID is always 300 in the function which is the end of the loop. IForm is executed when the $ID in the loop lines up with the shortcode ID on the site/post which tells me the $ID value is indeed correct for some part of the loop. When the ID is indeed correct I would like to pass it to the IForm function to use it to find the right form in the database(MySQL).
Now my question is how would I pass (if that can even be done) the $ID value on the 3rd row of the loop to the 5th row of function. Alternatively would be to force break the loop when it lines up and use the last $ID value to be passed to IForm.
WordPress makes this a little more complicated than it needs to be, because you can't pass additional parameters to a shortcode (afaik).
Before we start, let's understand the 300. The loop in which you add the shortcodes gets executed during an early stage of the page load. The actual call to your shortcode function at a later stage. At that time the global $ID variable will have its final value (300 in your case)
Here are two ways to solve this:
First, you can use closures, and inherit $ID from the parent scope:
for ($x = 1; $x <= 300; $x++) {
$ID++;
$ShortCode = "IForm_";
$ShortCode .= $ID;
$FormID = $ID;
add_shortcode( $ShortCode, function () use ($ID) {
$DBForm = getDB($ID);
$Form = $DBForm;
$Form .= "Works but not really";
return $Form;
} );
$ShortCode ='';
}
You can read more about it in the manual. It also explains the difference between use and global variables.
Second, you can use shortcode attributes. You would not use [Form_1] anymore, but [Form id=1]. Then you can access the id within the array that Wordpress automatically passes to your function.
function IForm($attr){
$ID = intval($attr['id']); // A little sanitation, because $attr could come from a user with low privileges
$DBForm = getDB($ID);
$Form = $DBForm;
$Form .= "Works but not really";
return $Form;
}
A little documentation about this is available here.
Lets see what you did there
$ID= 0;
// Define a variable called ID
for ($x = 1; $x <= 300; $x++) {
global $ID;
// get access to global variable id
$ID++;
// increment local or global counter, undefined behaviour
instead i recommend doing
global $ID;
for ($x = 1; $x <= 300; $x++) {
$ID++;
// Do what you think you have to do
do not pass variables over global variables into functions, instead use parameter
function IForm($id){
//testing_decode();
$DBForm = getDB($id);
$Form = $DBForm;
$Form .= "Works very good";
return $Form;
}
and call the function like
IForm(42); //or
IForm($ID);
I am trying to add multi language support for my website, like this:
Language.php:
class lang {
private $lang = null;
function __construct($lang) {
global $module;
$module['lang'] = true;
$this->lang = parse_ini_file("includes/languages/{$lang}.ini");
}
public function xlate($str) {
$arg_count = func_num_args();
if ($arg_count > 1) {
$params = func_get_args();
// strip first arg
array_shift($params);
} else {
$params = array();
}
$out_str = isset($this->lang[$str]) ? $this->lang[$str] : null;
if (!$out_str) {
throw new exception("Lang String Not Found: $str");
$this->lang = parse_ini_file("includes/languages/en_gb.ini");
}
return vsprintf($out_str, $params);
}
}
I then have my language variables stored in a ini file:
en_gb.ini:
Support = Support
Quick Menu = Quick Menu
HOME_MAIN_TITLE = You will Get Paid
Then I use it like:
echo $lang->xlate("Support");
echo $lang->xlate("Quick Menu");
echo $lang->xlate("HOME_MAIN_TITLE");
Which works. However, whenever I pass a ' character in the language, it doesn't work:
HOME_MAIN_TITLE = You'll Get Paid
It echoes the text until the ' character:
You
And then it stops.
How can I fix this?
In general, all strings in .ini file should be wrapped into "double quotes". This will fix issue.
May I suggest you to look at something generic and community approved. Like https://github.com/symfony/translation (it has .ini loader inside)
How do I share same variable between independent functions? I don't want to use globals and at the moment, I'm not using OO. This example only works within nested functions:
$example = function() use ($id){
echo 'id is: ' . $id;
};
Is there a way to access (read: to call) that nested function from a different function? I could then return $id.
Main issue: I'm retrieving an ID from a database and I want the moderator to be able to edit material based on the ID that is being retrieved from the database.
As mentioned in my comment , I'm not 100% what exactly you mean by independent and nested function but here is PHP's manual on (anonymous function)[http://php.net/manual/en/functions.anonymous.php].
Anyway I have put a sample together assuming you are really meaning nested functions
<?php
function wrapperOne($id) {
$newId = function($id) {
return $id * 2;
};
return $newId($id);
}
function wrapperTwo($id) {
$doSomething = function() use(&$id) {
$id *= 3;
};
$doSomething();
return $id;
}
echo wrapperOne(2); // 4;
echo PHP_EOL;
echo wrapperTwo(2); // 6;
You can use passing by reference read this
function changeMyID(&$id)
{
$id = 1;
}
function printMyID($id)
{
echo $id;
}
$id = 2;
changeMyID($id);
printMyID($id); // this will output 1;
I have a function that takes an input variable and outputs a template with the following call:
outputhtml($blue_widget);
outputhtml($red_widget);
outputhtml($green_widget);
And a simplified version of the function:
function outputhtml($type)
{
static $current;
if (isset($current))
{
$current++;
}
else
{
$current = 0;
}
//some logic here to determine template to output
return $widget_template;
}
Now here is my problem. If I call the function in a script three times or more, I want the output to be one way, but if I only call the function twice, then I have some html changes that need to be reflected in the templates that are returned.
So how can I modify this function to determine if there are only two calls for it. I can't go back after the fact and ask "hey function did you only run twice???"
Having trouble getting my head around how I tell a function that it is not going to be used after the second time and the necessary html modifications can be used. How would I go about accomplishing this?
function outputhtml($type)
{
static $current = 0;
$current++;
//some logic here to determine template to output
if ($current === 2) {
// called twice
}
if ($current > 2) {
// called more than twice
}
return $widget_template;
}
That would not be practical using a static $current inside the function; I would suggest using an object to maintain the state instead, like so:
class Something
{
private $current = 0;
function outputhtml($type)
{
// ... whatever
++$this->current;
return $template;
}
function didRunTwice()
{
return $this->current == 2;
}
}
The didRunTwice() method is asking "did you run twice?".
$s = new Something;
$tpl = $s->outputhtml(1);
// some other code here
$tpl2 = $s->outputhtml(2);
// some other code here
if ($s->didRunTwice()) {
// do stuff with $tpl and $tpl2
}
The only way you can find out if a function was only called twice is by putting the test at the end of your code; but perhaps by then the templates are no longer accessible? Can't tell much without seeing more code.
So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database.
Here's a brief example:
// template is stored in db, so that's how this would get loaded in
$template = "Hello, %customer_name%, thank you for contacting %website_name%";
// The array of replacements is built manually and passed to the class
// with actual values being called from db
$replacements = array('%customer_name%'=>'Bob', '%website_name%'=>'Acme');
$rendered = str_replace(array_keys($replacements), $replacements, $template);
Now, that works well and good for single var replacements, basic stuff. However, there are some places where there should be a for loop, and I'm lost how to implement it.
The idea is there'd be a template like this:
"hello, %customer_name%, thank you for
requesting information on {products}"
Where, {products} would be an array passed to the template, which the is looped over for products requested, with a format like:
Our product %product_name% has a cost
of %product_price%. Learn more at
%product_url%.
So an example rendered version of this would be:
"hello, bob, thank you for requesting
information on:
Our product WidgetA has a cost of $1.
Learn more at example/A
Our product WidgetB has a cost of $2.
Learn more at example/B
Our product WidgetC has a cost of $3.
Learn more at example/C.
What's the best way to accomplish this?
Well, I really dont see the point in a template engine that uses repalcements/regex
PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}
Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.
The way i would implement a template engine is like so
Firstly create a template class
class Template
{
var $vars = array();
function __set($key,$val)
{
$this->vars[$key] = $val;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : false;
}
function output($tpl = false)
{
if($tpl === false)
{
die('No template file selected in Template::output(...)');
}
if(!file_exists(($dir = 'templates/' . $tpl . '.php')))
{
die(sprintf('Tpl file does not exists (%s)',$dir));
}
new TemplateLoader($dir,$this->vars);
return true;
}
}
This is what you use in your login such as index.php, you will set data just like an stdClass just google it if your unsure. and when you run the output command it sends the data and tpl to the next class below.
And then create a standalone class to compile the tpl file within.
class TemplateLoader
{
private $vars = array();
private $_vars = array(); //hold vars set within the tpl file
function __construct($file,$variables)
{
$this->vars = $variables;
//Start the capture;
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean(); //Clean it
//Return here if you wish
echo $contents;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
}
function __set($key,$val)
{
$this->_vars[$key] = $val;
return true;
}
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
}
The reason we keep this seperate is so it has its own space to run in, you just load your tpl file as an include in your constructor so it only can be loaded once, then when the file is included it has access to all the data and methods within TemplateLoader.
Index.php
<?php
require_once 'includes/Template.php';
require_once 'includes/TemplateLoader.php';
$Template = new Template();
$Template->foo = 'somestring';
$Template->bar = array('some' => 'array');
$Template->zed = new stdClass(); // Showing Objects
$Template->output('index'); // loads templates/index.php
?>
Now here we dont really want to mix html with this page because by seperating the php and the view / templates you making sure all your php has completed because when you send html or use html it stops certain aspects of your script from running.
templates/index.php
header
<h1><?php $this->foo;?></h1>
<ul>
<?php foreach($this->bar as $this->_foo):?>
<li><?php echo $this->_foo; ?></li>
<?php endforeach; ?>
</ul>
<p>Testing Objects</p>
<?php $this->sidebar = $this->foo->show_sidebar ? $this->foo->show_sidebar : false;?>
<?php if($this->sidebar):?>
Showing my sidebar.
<?php endif;?>
footer
Now here we can see that were mixing html with php but this is ok because in ehre you should only use basic stuff such as Foreach,For etc. and Variables.
NOTE: IN the TemplateLoader Class you can add a function like..
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
This will allow you to increase your actions in your templates so bold,italic,atuoloop,css_secure,stripslashs..
You still have all the normal tools such as stripslashes/htmlentites etc.
Heres a small example of the bold.
$this->bold('foo'); //Returns <strong>somestring</string>
You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource
If you have any more questions feel free to ask me.
----------
An example of storing in your database.
<?php
if(false != ($data = mysql_query('SELECT * FROM tpl_catch where item_name = \'index\' AND item_save_time > '.time() - 3600 .' LIMIT 1 ORDER BY item_save_time DESC')))
{
if(myslq_num_rows($data) > 0)
{
$row = mysql_fetch_assc($data);
die($row[0]['item_content']);
}else
{
//Compile it with the sample code in first section (index.php)
//Followed by inserting it into the database
then print out the content.
}
}
?>
If you wish to store your tpl files including PHP then that's not a problem, within Template where you passing in the tpl file name just search db instead of the filesystem
$products = array('...');
function parse_products($matches)
{
global $products;
$str = '';
foreach($products as $product) {
$str .= str_replace('%product_name%', $product, $matches[1]); // $matches[1] is whatever is between {products} and {/products}
}
return $str;
}
$str = preg_replace_callback('#\{products}(.*)\{/products}#s', 'parse_products', $str);
The idea is to find string between {products} and {products}, pass it to some function, do whatever you need to do with it, iterating over $products array.
Whatever the function returns replaces whole "{products}[anything here]{/products}".
The input string would look like that:
Requested products: {products}%product_name%{/products}