Archive

Archive for the ‘PHP Development Notes’ Category

PHP Functions to work with PHP functions

July 9th, 2009 Zareef Ahmed No comments

In this article, I am going to write about the functions using which you can get the information about php functions.

For a full list of PHP functions you can visit http://www.zend.com/phpfunc/

Using these functions you can query the stats/status of your functions, these functions are very useful in dynamic application creation.

For example, using func_get_arg and func_num_args functions you can simulate the function overloading in PHP.

Read a good example for http://www.dubi.org/php-function-overloading

Functions I will write in this article are :-

get_defined_functions();

You want to how many functions are in your installation of PHP, just call this function, it will return an array of all php functions you can call. For the convince it return an multidimensional array, so you can access both internal and user defined functions separately.

func_get_arg()

Returns the argument which is at the arg_num’th offset into a user-defined function’s argument list. Function arguments are counted starting from zero. func_get_arg() will generate a warning if called from outside of a function definition. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function.
If arg_num is greater than the number of arguments actually passed, a warning will be generated and func_get_arg() will return FALSE.
As this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable.

func_get_args()

Returns an array in which each element is a copy of the corresponding member of the current user-defined function’s argument list. func_get_args() will generate a warning if called from outside of a function definition. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function.
This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.
As this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable.

func_num_args() Returns the number of arguments passed into the current user-defined function. func_num_args() will generate a warning if called from outside of a user-defined function. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function.
As func_get_arg() this function also can not be passed as argument to another function.

function_exists()

Checks the list of defined functions, both built-in (internal) and user-defined, for function_name. Returns TRUE on success or FALSE on failure.

create_function()

Creates an anonymous function from the parameters passed, and returns a unique name for it. Usually the args will be passed as a single quote delimited string, and this is also recommended for the code. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, if you use double quotes there will be a need to escape the variable names, e.g. \$avar.

This is one of very useful function to create dynamic application, using this function you can create the function at run time and then use them as per your requirements.

call_user_func()

Call a user defined function given by the function parameter.
People are divided on the use of this function, some thinks that it is not needed at all when we can run the function on the variable itself.
Like use of this function is

function test($value)
{
print "This is $value";
}

call_user_func("test","test value");

some people prefer using variable function like this :-

$funcname="test";
$funcname("test");

This is also a call_usr_func_array function.

is_callable()

If you are going to use a a variable function like previous example, you may need to verify if a function already present and callable? using this function you can do that.
This function Verify that the contents of a variable can be called as a function. This can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name.
The var parameter can be either the name of a function stored in a string variable, or an object and the name of a method within the object, like this:

array($SomeObject, 'MethodName')

In some way it is similar to function_exisits function but it does more that that if you just want to check the syntax then you can pass a second argument as True, in that case it only verifies that var might be a function or method. It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback. The valid ones are supposed to have only 2 entries, the first of which is an object or a string, and the second a string.

PHP is a functional language and you can almost do anything with these functions.

Tags:

Use Exit after redirecting user via header function

June 18th, 2008 Zareef Ahmed No comments

Some weeks ago I got an old PHP application to work with. I was asked to look for the potential problems and bottlenecks in the application. So I decide to use APD as a profiler for the application, after installing the APD on that server. I enabled profiler on one of the reportedly slow page.

That page was meant to be displayed and executed only if user is logged in and session variables are set. If we don’t found user session then user was being redirected to Login page of the application.

I called that page directly in the browser and got the login screen. It was perfectly acceptable behavior.

BUT when I run the profiler to get the information about the execution of page I was surprised to see that almost all functions which were only meant to be executed for logged in users were being called and they were taking the system resources.

Just to confirm I refresh the page in browser many time (without login), and each time I got same profile information for page.

Then I run the test using Apache benchmark (ab) and while test was in running I decide to see the process list of mysql database, again I see that heavy queries were being fired on the database. (But this time I was not surprised, as it was expected and I did just to confirm and take the screen shot so I can show that to the owner of the application.)

Now I decide to check the reason behind this, so I opened that page in my Zend Studio, and yes I got the reason …

Application was redirecting the user to login page if user is not logged in using header function via location define mechanism Like

  header("location: login.php");

And they were not using exit after that. I just add the exit(); after that header function call and problem was fixed.

header("location: login.php");
exit();

Just an improper use of header was the reason behind unnecessary load on the web server as well as on database server.

Note : If you do use the location setting of the header to redirect to another page, do not forget to use “exit;” to prevent further execution of script code.

Tags:

Using SQL abstraction and CRUD factory classes is a good practice?

July 6th, 2007 Zareef Ahmed No comments

This question again struck me today, somebody just throw a simple question about EXPLAIN statement in MySQL and in hast I relate that to DESCRIBE. When that person cut the line of phone, I suddenly realize my mistake.

I am working with PHP and MySQL since last seven years then why this happen to me? This is because I am not using MySQL frequently, although it is the integral part of almost every application I am working on. I am fairly away from MySQL queries because I am using my factory classes or framework like cakephp which does not require me to write the SQL queries. Almost all of my operations on database are covered within my CRUD classes. Even complex select or updates are possible using simple search objects in my factory classes or framework I am using.

BUT this practice has caused a deficiency in my knowledge of MySQL statements. This situation is really horrible sometime. Do I need to stop using my factory classe practice and abstraction. I normally prefer abstraction and factory classes because I want to make my code portable to other databases also.

I am really a in fix What should I do?
BUT again another question struck me What is more important? My knowledge of MySQL statements or fast and portable execution of projects. When we got the higher level, we certainly miss the lower level details of applications.

So I will stick to my factory classes, because I know I may struggle with some academic questions about MySQL for the time being but at least I can do the stuff with just one reference to MySQL manual. Above all I may face problem in simple MySQL statements but I am good at high level things like replication and performance tuning at server level.

What you say is it a good decision?

Tags:

PHP4 and PHP5 version conflicts and why we should stop using PHP 4

June 14th, 2007 Zareef Ahmed No comments

PHP 5 is in the market since many years, but it still has to capture the market.

Why? Is it not efficient version of PHP language?

What are main reasons behind slow-adoption of PHP version 5.

I am compiling a list of reasons; you can also send me the reason why you have moved to PHP5 or why you are not able to do that?

I will write about that list in my coming articles.

Tags:

How to create a file upload progress bar in any version of PHP?

May 19th, 2007 Zareef Ahmed No comments

In this article I am going to write my experience with a work in which I was supposed to do file upload progress bar.

First of all I consider using APC then came to know about PECL extension uploadprogress

Unfortunately both were not possible for me as I have no control on the server on which it was supposed to run.

Tweaking with installed version of PHP or up gradation of PHP was also ruled out.

In India we have a word which is very common in use whenever someone is in a deadlock, the magic word is ‘Jugad’, if simply translated into English, it will mean ’system’, but in contextual meaning it stands for ‘Smart Work’.

Well I can not claim that adjective for this work, as it has some contextual limitations but it is working solution.

Before I jump to the final details of the working structure, I would like to recollect some facts about file uploading in PHP.

Whenever you upload something via a PHP page, it gets uploaded to a temporary folder on server then programmer has to move it from there to a proper location via PHP script.

That temporary folder can be defined in php.ini file and if not defined PHP uses operating systems default temporary folder.

I have created this script with the help of Ajax.

First of all I have changed temporary folder of PHP to a folder on which I have some control so I can access that with my PHP scripts time to time. Then I have created a form to upload the file to server.

I created another PHP script to calculate the size of a particular folder and THATS’S IT application was finished.

Whenever someone hits the upload button, file upload starts to the server.Using Ajax I call my other script which that calculates the size of temp folder after every second, and then I show the updated html page with file size using Ajax.

You can call it dirty programming but it is working solution to this problem in any version of PHP.

When this solution will not work?

It will always work but in some situations may give you wrong results. This approach will not produce desired results on the servers where lots of file handling is being done via PHP and lots of people are doing file upload at one go.

Although it can be designed to have multiple file upload at same time like instead of temporary folder we can opt for calculation of particular temporary file in temporary folder and that file can be guessed by the upload button hit time, but that is not 100% usable in every situation. If lots of people are uploading the files at same time it may conflicts with each other.

Did you like this Jugad?

Tags: