Asked  8 Months ago    Answers:  5   Viewed   42 times

I have a business logic classes that are written in pure C# (without any specific things from this language) and I would convert this code into PHP. I can write my own parser, but think if I could someone did it before me.

Could you please tell me where can I find this kind of converter?

Ps. As I've written I use only plain C# programming in this language. Only arguments, declarations of variables, equations and control statements.

 Answers

23

I know you're hoping for someone who had experience but in case no one comes forward...

You might consider just copy and pasting the code into a PHP script and checking what breaks. Write a parser to fix that, run it across the entire script, and see what's the next thing that breaks. Continue until the script functions as expected.

If you're not using any of the more involved .Net classes I can't imagine you'll have too much trouble.

Wednesday, March 31, 2021
 
Asnexplore
answered 8 Months ago
50

You actually should ensure that you type what you mean:

$bigint = 9999999999999999999;

Is not a PHP integer but float:

float(1.0E+19)

If you would have done

$bigint = (int) 9999999999999999999;

You would have set an integer in fact, but it would not be the number you might have expected:

int(-8446744073709551616)

It's no problem at all to turn that into string as you might have guessed. So take care when you write numbers into code that you actually write what you mean.

See this line again:

$bigint = 9999999999999999999;

try to understand what you have actually written. It's not an integer at all because PHP will turn it into a float. See Example #4 Integer overflow on a 64-bit system in the integer manual page.

If you need higher precision, checkout GNU Multiple Precision, it might have what you're looking for. However, this won't change how to write numbers in PHP:

$bigint = gmp_init("9999999999999999999");
$bigint_string = gmp_strval($bigint);
var_dump($bigint, $bigint_string);

Output:

resource(4) of type (GMP integer)
string(19) "9999999999999999999"
Wednesday, March 31, 2021
 
nasty
answered 8 Months ago
48

A tokenizer is not the same as a parser. The tokenizer just produces tokens and not in a tree format. For those who are actually looking to produce an AST for PHP similar to what ruby_parser does for Ruby, use the PHP-Parser project (https://github.com/nikic/PHP-Parser).

The PHP-Parser project also comes with a pretty printer that turns your AST back to PHP.

Wednesday, March 31, 2021
 
keyBeatz
answered 8 Months ago
95

Did you even try to implement that yourself or read at http://php.net/manual/en/function.header.php?

Write those lines before the first output of the file.
Should do the job (although untested):

<?php
// ...
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: origin, x-requested-with, content-type');
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
?>
Wednesday, March 31, 2021
 
kmunky
answered 8 Months ago
34

I am surprised this wasn't mentioned yet: what about using Zend_Reflection of Zend Framework? This may come in handy especially if you work with a software built on Zend Framework like Magento.

See the Zend Framework Manual for some code examples and the API Documentation for the available methods.

There are different ways to do this:

  • Pass a file name to Zend_Reflection_File.
  • Pass an object to Zend_Reflection_Class.
  • Pass an object and a method name to Zend_Reflection_Method.
  • If you really only have the comment string at hand, you even could throw together the code for a small dummy class, save it to a temporary file and pass that file to Zend_Reflection_File.

Let's go for the simple case and assume you have an existing class you want to inspect.

The code would be like this (untested, please forgive me):

$method = new Zend_Reflection_Method($class, 'yourMethod');
$docblock = $method->getDocBlock();

if ($docBlock->hasTag('return')) {
    $tagReturn = $docBlock->getTag('return'); // $tagReturn is an instance of Zend_Reflection_Docblock_Tag_Return
    echo "Returns a: " . $tagReturn->getType() . "<br>";
    echo "Comment for return type: " . $tagReturn->getDescription();
}
Sunday, August 1, 2021
 
Heisenberg
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :