Would setting a function name by a variable be valid PHP? - php

I know this code works but I don't know if it'd be valid to do. The reason I wanted to support this is to allow people to overwrite this function with another file. Sounds slightly strange, but for my purpose it makes sense.
I only ask because I seriously can't find an example of this being done and have been searching for an hour or so.
$jerry = function(){
return 'hello world';
};
echo $jerry();

Yes, it is valid to use anonymous functions assigned to variables.
You should make use of the PHP documentation (under "Language Reference" > "Functions" > "Anonymous functions") when you are not sure about something. The website is actually quite easy to use.
Another way of finding stuff on php.net is to use the URL path. For example, if you are interested in functions then you can just go to php.net/functions, and it will attempt to direct you to the right place. This will work with quite a lot of stuff; php.net/juggling, for instance, will direct you to the "type juggling" page. If it is unable to direct you to something relevant then it will show you search results instead, using the URL path as the search query.

I believe you can do something else... say you have
function1() {
//code for function1 here
}
function2() {
//code for function2 here
}
you can actually do this
$var = 'function1';
$var(); //runs function1()
$var = 'function2';
$var(); //runs function2()
Hope it clears out things
Otherwise all the information you seek is in www.php.net/functions as the fellow user pointed out earlier. Learn to search php.net for any issues, at the bottom of every documentation page there are EXAMPLES that 95% help you for what you're looking to achieve

Related

How to encode php functions

I would like to encode a php page which contains some php functions.
For example, I have a page named: code.php with this functions:
<?php
function data(){
echo "foo";
...
}
function storage(){
echo "storage files..";
...
}
?>
I use these functions in my other php pages and I would like to protect them by other users. How can I encode their code?
I read about base64_encode() but the examples only show how to encode a string: how can I use this solution to encode and decode my php functions?
Thank you!
If you want to stop others from seeing your PHP code you can either make it as hard as possible (via minifying, obfuscating, whatever you wish to call it) or encrypt it.
There's an answer right here on SO with a few suggestions and another I'd add is ion cube.
With encrypted code you're likely to need further changes to your web server such as an apache module. With obfuscation it will just make it harder for the other developers to read, for instance changing variables and functions names to something meaningless and hard to read.
You will inevitably need to keep a copy of your unobfuscated PHP so you can work on it in a sane manner, which may be hard if you're only developing on your server.
To use Base64 you're probably thinking of doing something like this:
eval(base64_decode('ZnVuY3Rpb24gZGF0YSgpew0KZWNobyAiZm9vIjsNCn0NCmZ1bmN0aW9uIHN0b3JhZ2UoKXsNCmVjaG8gInN0b3JhZ2UgZmlsZXMuLiI7DQp9DQokZGF0YSA9ICdkYXRhJzsNCiRzdG9yYWdlID0gJ3N0b3JhZ2UnOw=='));
What's happening here is the Base 64 string is actually valid PHP, and you first decrypt it the eval it. An example of what the decoded string might look like:
function data(){
echo "foo";
}
function storage(){
echo "storage files..";
}
$data = 'data';
$storage = 'storage';
After the above eval call you would then do something like:
// call the data function
$data();
// call the storage function
$storage();
As stated from the documentation:
PHP supports the concept of variable functions. This means that if a
variable name has parentheses appended to it, PHP will look for a
function with the same name as whatever the variable evaluates to, and
will attempt to execute it.
So, calling $someVariable() will try to run a function named whatever $someVariable contains. If you set $someVariable to foo, it would try to run foo(), if you set $someVariable to sausage, it would try to run sausage() and so on.
Obviously bear in mind that you need to make sure these function variables' names aren't going to be used elsewhere.

What is getScenarioPath and getScenarionResourceFolder?

I am looking at someones project and keep getting this line:
$resource_folder = getScenarioResourceFolder(getScenarioPath($scenario));
I cannot find any function that he implemented under those two names - getScenarioResourceFolder and getScenarioPath.
I started wandering that maybe the name Scenario has something to do with $scenario variable being in those functions. I know it might sound dumb, but I do not know what else to think.
Does anyone know about these function?
I know these:
You can search the function:
http://id1.php.net/manual-lookup.php?pattern=getScenarioPath if no
result, That is must be user-defined function.
You can check yourself by using
if (function_exists('getScenarioPath')) {
...
} else {
...
}
These are clearly custom functions that have been written in.
The simplest solution would be to GREP your entire system for getScenarioResourceFolder - you are looking for a .php file.
If you can't grep or don't know how, then it's time to go digging. Open any PHP files that are related to that project and look for getScenarioResourceFolder().
If you really don't have it, then you'll have to get in touch with the original architect of the project.

Monitor a PHP variable for change in value

I am actually trying to monitor a PHP variable (may be as a separate thread but not possible in PHP) and fire a PHP function whenever the value of the variable changes.
eg: lets take a variable $_GLOBALS['foo']=1;
if at any point in the code, the value of $_GLOBALS['foo'] changes to something else, i want to fire a PHP function immediately.
The variable can be anywhere inside a loop or in a function,etc.
Why i want this: I have a variable which stores the last error occured as a text. If the value of the variable changes from "" to something else, i want to trigger an error. My LOGIC may seem a bit strange but this is what i would like to do.
Thanx in advance.
Edit: I tried: How to use monitors in PHP? and How can one use multi threading in PHP applications but does not seem to solve the problem.
The Code (Thought this could solve some of your doubts on my question):
public function addtag($tagarray,$uid,$tagtype="1")
{
$dbobj=new dboperations();
$uiobj=new uifriend();
$tagid=$uiobj->randomstring(30,DB_SOCIAL,SOCIAL_TAG,tag_tagid);
$c=0;
foreach($tagarray as $tags)
{
$c++;
$tagname=$tags["tagname"];
$taguid=$tags["tagid"];
$dbobj->dbinsert("INSERT INTO ".SOCIAL_TAG." (".tag_tagid.",".tag_fuid.",".tag_tuid.",".tag_tagname.",".tag_tagtype.") VALUES
('$tagid','$uid','$taguid','$tagname','$tagtype')",DB_SOCIAL);
}
if($c==0)
{
$lasterror="No tags were added";return "";
}
else
{
return $tagid;
}
}
Here, if i call a error handling function instead of monitoring the variable, it wont be advisable in my case since the error handling function may do any operation like give alert and redirect to a page or any similar operation.
I asked this question cause, i thought what if the script does not reach the line
return "";
It would affect the project's workflow. Thats what i am worried about.
And the variable i was talking about is $lasterror and i have many functions like this where $lasterror is used.
I saw this, so I built this:
https://github.com/leedavis81/vent
Should solve your problem.
There is no built-in way to do this in PHP, and there's no easy way to add it. It doesn't really feel right for the way the language works anyway.
Instead of setting a variable, you could build a custom function that handles the error - or use PHP's built-in error handling functionality using a custom error handler.
Another error handling method which comes close to what you want to do (I think) is exceptions.

How can I reference variables from another included file in PHP?

So I'm working on a PHP app and trying to make everything moduler. I have an index.php file that includes other php files. The first file included is settings.php which has my postgres credentials defined so they can be accessed elsewhere. The second file is connect.php that has a function you can pass sql to and it will return $result. The third file has functions that call the sql function and receive $result and parse it. In the third file, I can read the results of the $result however if I try if($result) it breaks and isset/empty have no effect.
Anyone have any ideas on a way to make this work, or is my structure just terrible?
Thanks so much!
Mike
let's say you have the following three files:
inc1.php
<?php
$foo = 'hello';
?>
inc2.php
<?php
echo $foo;
?>
main.php
include('inc1.php');
include('inc2.php');
it should echo "hello". however, passing variables around among files is a bad idea, and can lead to a lot of confusing, hard-to-follow code. If you need to pass variables around, use functions and/or objects so that you can at least see where they are coming from.
beyond that though, it's difficult to tell exactly what your problem is without seeing the code in question.
I would really try to switch to OOP. This makes things a lot of easier. If you just have to deal with classes, their methods and attributes you only have to include the classes and not this choas of functions. So I would recommend, give it a go ...

Is it possible to know where variable were set?

I was wondering if it is possible to see where variable were set in php code?
That would make debugging really easier as i assign different values to same variable based on some condition.
Nope.
However, many good PHP IDEs (at least NuSphere's phpEd, I'm sure Zend must have that too?) offer a possibility to jump to the point at which the variable was first used, and highlight all its occurrences.
If you are wondering about debugging. Do this.
if(condition)
{
echo "1";
$var = "something";
}
elseif(othercondition)
{
echo "2";
$var = "something";
}
...
Etc, this is really quickly done and you can see the number quickly in the output or a log file.
Sure. In Notepad++ you can install the SourceCookifier plugin.
From the website: "A plugin which uses Exuberant Ctags to parse either only the currently activated source file or multiple files of so-called sessions. The results are shown and can be browsed in a treeview inside of a dockable window."
From my own experience: it just works - for variables, functions, properties, methods, classes, interfaces... and some HTML and javascript stuff too.

Categories