PHP : Can I compile/build my site like .NET? - php

I am new to PHP, and working on a class based web site. So my question is simple. Can I compile the site like I can in .NET?
This would be helpful to me because I changed my IsAAction interface to accept an error callback function as a parameter of the Execute function, and I would like all of my implementations to break so I can easily see where to change them.
Thanks in advance.

PHP can be compiled, as in you can compile it to machine code and prevent a parsing everytime you access a file. That the way accelerator like APC work. But you can't compile it like a .NET DLL with every error checked in advance. It would go against its dynamic nature. Late binding mean it will always check for error at runtime
For your particuliar problem, I would suggest adding Unit Tests to your project. They are very good to have anyway, and they will help you catch error in your interface implementation. I personally couldn't live without them. You may want to check PHPUnit or SimpleTests.
EDIT To understand why PHP can't check your error, just check this snippet of code.
if ($_GET['impl'] == 'interface1') {
interface MyInterface {
function foo($var);
}
} else {
interface MyInterface {
function bar($var, $var2);
}
}
class Implementation implements MyInterface { //Will crash or not depending on the URL!
function foo($var) {}
}

Yes and no. See:
Can you "compile" PHP code?
And:
http://www.phpcompiler.org/
And:
http://www.scriptol.com/apollo.php

https://github.com/facebook/hiphop-php/wiki - although I'm sure there are other answers on here that you'd find useful.

There's a opened topic on stackoverflow that is related.
Can you “compile” PHP code?
Some parties such as Facebook rewrote PHP runtime, or part of it so they make it possible to run compiled php code or other source codes, pretty much whatever you like.
HipHop for PHP
Its not an easy task. But you can do it.

Not to my knowledge. PHP is a script language.

Related

Is there any `debug_backtrace()` function analogue to watch call stack in zf2 or is there any approach to debug call trace in zf2?

Introduction
I am getting strange error in zf2. I don't know why, but I am getting some routing error. I don't get any messages, but needed controller is not loaded, but another one do. All looking fine, so it is not good idea to ask here to help me to solve this problem, so I am trying to debug this by myself.
What I have tried?
I tried debug_backtrace() function in /public/index.php file like this:
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
var_dump(debug_backtrace());
It shows nothing.
Just to check if function is working I tried:
function bob() {
var_dump(debug_backtrace());
}
bob();
And this showed me what params were passed, what functions called etc. This showed me, that function is executed correctly, but this function is not used in my case.
Question
Is there any ways to get what functions was executed in app?
I think you slightly misunderstand what debug_backtrace() does. It lists the function calls that led to the place in code where the backtrace is. So in your bob() example, it will show that function (as bob() was called). But if you just call debug_backtrace() in public/index.php, that's not executed within any functions, which is why you don't get any output.
I am not sure how reasonable it will be for you to install a different stack, but ZendServer has code tracing/profiling built in and that is exactly what you are looking for.

Do PHP variable functions work in PHP4?

I know you can do something like this in PHP5:
function sayHi() {
echo "Hi!";
}
$func = "sayHi";
$func();
It is called a variable function in the documentation. But the docs don't say anything about what versions of PHP this works on. This could mean it works on ALL versions of PHP, but I doubt it. Specifically, does this work on PHP4?
For php 4.3 they are working. Build-in function test here, your source test here.
Definitely Yes. As you can see in here when there is a dependency in a certain function they set it below the title.
No dependcy in version of php
VARIABLE Functions
With dependency in version of php
INTVAL Function
note: so whenever you have concerns on a certain function that you will use regards on your php version better consult php.net for info.

How can I check a php file for undefined functions?

Calling php -l checks the syntax of a php file.
Is there a way to check for undefined functions? I don't need it to work with functions defined at runtime nor with dynamic calls.
Calling the file obviously won't work: I need to check the whole file, not only the branch that gets executed. See the follwing example.
myfile.php:
function imhere () {}
main.php:
require_once 'myfile.php';
if (true) {
imhere();
}
else {
imnhothere(); // I need to get a warning about this
}
Checking for undefined functions is impossible in a dynamic language. Consider that undefined functions can manifest in lots of ways:
undefined();
array_filter('undefined', $array);
$prefix = 'un'; $f = $prefix.'defined'; $f();
This is in addition to the fact that there may be functions that are used and defined conditionally (through includes or otherwise). In the worst case, consider this scenario:
if(time() & 1) {
function foo() {}
}
foo();
Does the above program call an undefined function?
Check the syntax only need to scan the current content of the php file, but check for function defined or not is something different because functions could be defined in the required file.
The simplest way is just to execute the file, and undefined function will give you a PHP Fatal error: Call to undefined function .....
Or you may want this.
Following comment tennis I thought I would wrap up some thoughts into an answer.
It seems like what you might need is some higher-level testing (either integration or web testing). With these you can test complete modules or your whole application and how they hang together. This would help you isolate calls to undefined functions as well as a host of other things by running tests against whole pages or groups of classes with the relevant includes statements. You can PHPunit for integration testing (as well as for unit-) although I'm not sure that would gain you that much at this point.
Better would be to investigate web-testing. There are a number of tools you could use (this is by no means intended to be a complete list):
Selenium which I believe can hook into PHPUnit
SimpleTest, not sure how well maintained this is but I have used it in the past for simple (!) tests
behat, this implements BDD using Gherkin but I understand it can be used to do web-testing (behat with Goutte

PHP runkit_function_rename don't work?

This code don't work. Why not?
<?php
function test()
{
echo 'test';
}
runkit_function_rename('test', 'test2');
test2();
?>
What I really want is this. I'm using a system that have a function. When I'm on localhost I want that function to do something different. I want to override the function with own stuff.
All alternatives are welcome as well.
Do you have the PECL extension installed?
http://www.php.net/manual/en/runkit.installation.php
This » PECL extension is not bundled with PHP.
I never had any luck with Runkit either.
You asked for alternatives, and I can definitely recommend this one:
Patchwork
Patchwork is a PHP function-override library. In other words, it does much the same job as Runkit.
The main difference is that it is written in pure PHP - no extensions to install; just a require_once() at the top of your code.
The flip side of this is that because it's pure PHP, it can only replace functions defined within your program; ie it can't override a PHP built-in function like Runkit can. The example in your question will work fine with Patchwork, but trying to override a PHP function like mysql_query() is not possible.
However, unlike Runkit, it works perfectly, so if you can live with that limitation, I'd strongly recommend it.
Another alternative to Runkit that you might want to try is PHP Test Helpers. This is a PHP extension, and covers pretty much the same ground as Runkit. It's written by the same author as PHPUnit, so it should be pretty good. However I didn't have much joy when I tried to install this either, so I can't really comment on it much.
I note from your comments elsewhere on this question that you're running Windows (ie WAMP). Neither Runkit nor PHP Test Helpers are provided with Windows executables; in order to use either of them in Windows you need to compile the extension yourself from the C source code. For this reason, if you're on Windows, then Patchwork is your only sensible choice.
What I really want is this. I'm using a system that have a function. When I'm on localhost I want that function to do something different. I want to override the function with own stuff.
All alternatives are welcome as well.
function test() {
if($_SERVER['HTTP_HOST'] == 'localhost' {
// do one thing
} else {
// do other thing
}
}
If you're set on using runkit, you'd need to use runkit_function_redefine, not runkit_function_rename to make the same function do different things.
As explained earlier, it's probably best to differentiate inside of a function body regarding the value of $_SERVER['HTTP_HOST'].
Although I'd personally see this as bad style, you can even define function inside of other functions or blocks.
This snippet defines one function get_template_part():
if($_SERVER['HTTP_HOST'] == 'localhost' {
function get_template_part() {
}
} else {
function get_template_part() {
}
}
Unfortunately, this wouldn't help in your case, since get_template_part() is already defined outside your reach.
Someone might also experience that runkit_function_* functions are not working although the runkit library is installed correctly. This is because these functions are broken for some PHP versions (probably at least all 5.2.*) as can be seen here: https://bugs.php.net/bug.php?id=58205

Timing portions of a PHP script, including runtime of individual methods of objects?

I was looking for a reliable, clean way to do advanced runtime reports for specific portions of a PHP script. What I'd love to do is dynamically time and track every method call of every object during script execution, log this information, and create a table/report at the end of the script for output.
I've thought about doing something along the lines of, in pseudocode:
class MagicTimingClass redefines *ALL_CLASSES* {
public function __call($name, $args) {
log_start_time();
$return = run_the_method();
log_end_time();
return $return; // return original method results
}
}
I know I can't do this, and I've looked into runkit/classkit for PHP but I'm not sure this will help me do exactly what I'm looking for.
I'm hoping to get out a report so I can determine what methods are causing my script to bottleneck without temporarily modifying my classes for this level of debugging and then un-modifying again when I need the code to go to production.
If anyone has any thoughts or leads that would be great! Thanks in advance
You could use a profiler like Xdebug which would mean you wouldn't have to modify your code at all.

Categories