Monday, December 17, 2012

call_user_func_array with constructor

Hast Du jemals versucht eine Klasse mit dynamischen Konstruktor und Methoden-Parameter zu laden?

Wie zum Beispiel:
$className = 'MyClass';
$constructorParams = array('a', 'b', 'c');
$methodName = 'run';
$methodParams = array('x', 'y', 'z');
Wie würde man vorgehen? Der erste Gedanke endet wahrscheinlich so ähnlich:
$class = new $className();
$result = $class->$methodName();

Und hier fängt das Problem an. Die Parameter sind nicht übergeben und es besteht so keine Möglichkeit das zu tun.

Die Lösung für dieses Problem ist eine Kombination aus der Reflection-Klasse und dem user_call_func_array Aufruf.
$rc = new ReflectionClass($className);
if (null !== $rc->getConstructor()) {
$realClass = $rc->newInstanceArgs($constructorParams);
}
else {
$realClass = $rc->newInstance();
}
$result = call_user_func_array(array(
$realClass,
$methodName
), $methodParams);


Sunday, December 16, 2012

SAPRFC PHP 5.4


You have PHP 5.4 and SAPRFC doesn't work anymore? 

Since PHP 5.4 zend has changed their source code. If you want to compile SAPRFC again, you will get many failures. That means, your compile will fail completely.

But I wouldn't writing this blog post without that I would have known the solution!


First

Search the file "saprfc.c" by following command.


cd /; sudo find | grep "saprfc\.c"

Second

Open the file with "nano", "vim" or similars. Then goto line 47 and change:


"function_entry" to "zend_function_entry"

That's all! Now, you are able to compile SAPRFC again. Although with grumble.

I hope this is helpful for you.

Best practices to write php code

Today I will show you a few tricks how to write good php code. If you follow these tricks, your code is easy to read and easier to maintain.

1. The boy scout rule
2. Typehinting
3. Comment rules
4. Distinctive mark for crap
5. How should I begin to refactor?
6. KISS

The boy scout rule

The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. 

That is the best approach. I can confirm this from years of experience. For more information visit the following link. 


Typehinting

Use typehinting whenever it is possible! PHP supports objects and the array type. For an example take a look at the manual. 


This have an advantage too. Your IDE knows then which class is assigned to the function variable! Your IDE does not support  a correct autocompletion? Then try out PhpStorm.


Comment rules

  • Write your name on each comment for feedback
  • Mark functions that no longer to be used as deprecated. Use the @deprecated tag, explain why and enter the right name of the function class.
  • Describe your functions only when they do "unexpected" or confusing. 
  • Prefers to invest time for a meaningful and clear identification name of the function. It keeps comments shorter.
  • Write always phpDocs! PhpStorm can do it for you quickly!

Distinctive mark for crap

  • A function has more then 6 parameters
    • Solution: Revise your design or use bit-flags
  • Optional parameters are not in last position
  • You use hard coded strings or numbers in switches or if-conditions
    • Solution: Use speeking class constants
    • Advantage: You can change them in one place and search for it easier
  • Code is repeating itself similar or equal
    • Solution: Summarize it into one function
    • Advantage: You need only maintain code in one place. Imagine if you had the code 4 timtes in a file. Then you might have to fix a bug 4 times.
  • Your code of the function or class is very long
    • Solution: Make it shorter into more functions or classes

How should I begin to refactor?

  1. Try to understand the logic. What is the point of this class or this function?
  2. Complete order. Give classes, variables and functions useful names speaking for themselves.
  3. Reduce duplicate code. Summarize them together to a central location.
  4. Looking for distinctive mark for crap
  5. Now is the time to think about whether this is the right architecture. Should I choose a more suitable design pattern?

KISS

Yes KISS! If you want to build a castle, then first build a great tower and expanding gradually. 

Finally, a well-intentioned advice

Just write code which you really need! Everything else is useless and a waste of time and possibly expensive.