有没有一种简单的方法可以将 C# 类转换为 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way to convert C# classes to PHP?
提问by John
I am used to writing C# Windows application. However I have some free hosted PHP web space that I would like to make use of. I have a basic understanding of PHP but have never used it's object oriented capabilities.
我习惯于编写 C# Windows 应用程序。但是,我有一些免费的托管 PHP 网络空间可以使用。我对 PHP 有基本的了解,但从未使用过它的面向对象功能。
Is there an easy way to convert C# classes to PHP classes or is it just not possible to write a fully object oriented application in PHP?
有没有一种简单的方法可以将 C# 类转换为 PHP 类,或者是不可能在 PHP 中编写完全面向对象的应用程序?
Update:There are no reliance on the .NET framework beyond the basics. The main aim would be to restructure the class properties, variable enums, etc. The PHP will be hosted on a Linux server.
更新:除了基础之外,不依赖 .NET 框架。主要目的是重组类属性、变量枚举等。PHP 将托管在 Linux 服务器上。
采纳答案by John Downey
It is entirely possible to write a PHP application almost entirely in an object-oriented methodology. You will have to write some procedural code to create and launch your first object but beyond that there are plenty of MVC frameworks for PHP that are all object-oriented. One that I would look at as an example is Code Igniterbecause it is a little lighter weight in my opinion.
几乎完全可以用面向对象的方法来编写 PHP 应用程序。您将不得不编写一些程序代码来创建和启动您的第一个对象,但除此之外,还有许多面向对象的 PHP MVC 框架。我会以Code Igniter为例,因为在我看来它的重量要轻一些。
回答by John
A huge problem would be to replicate the .Net Framework in PHP if the C# class usses it.
如果 C# 类使用它,那么在 PHP 中复制 .Net Framework 将是一个巨大的问题。
回答by Vincent Robert
I don't know about a tool to automate the process but you could use the Reflexion API to browse your C# class and generate a corresponding PHP class.
我不知道自动化该过程的工具,但您可以使用 Reflexion API 浏览您的 C# 类并生成相应的 PHP 类。
Of course, the difficulty here is to correctly map C# types to PHP but with enough unit testing, you should be able to do what you want.
当然,这里的难点是将 C# 类型正确映射到 PHP,但是通过足够的单元测试,您应该能够做您想做的事。
I advice you to go this way because I already did a C# to VB and C++ conversion. That was a pain but the result was worth it.
我建议你这样做,因为我已经做了 C# 到 VB 和 C++ 的转换。那是一种痛苦,但结果是值得的。
回答by JProgrammer
If the problem is that you want to transition to PHP and you are happy to continue running on a windows server with .NET support you might consider wrapping your code using swig.
如果问题是您想过渡到 PHP 并且您很高兴继续在具有 .NET 支持的 Windows 服务器上运行,您可以考虑使用swig包装您的代码。
This can be used to generated stubs to execute from php and you can then go about rewriting the .NET code into PHP in an incremental fashion.
这可用于生成存根以从 php 执行,然后您可以以增量方式将 .NET 代码重写为 PHP。
This works for any of the supported languages. ie. you could incrementally rewrite an application in c++ to java if you really wanted to.
这适用于任何受支持的语言。IE。如果您真的愿意,您可以将 C++ 中的应用程序逐步重写为 Java。
回答by maetl
PHP doesn't support enums, which might be one area of mismatch.
PHP 不支持枚举,这可能是不匹配的一个方面。
Also, watch out for collection types, PHP despite it's OO features, tends to have no alternative to over-using the array datatype. Check out the sections on the PHP manual on iterators if you would like to see beyond this.
此外,注意集合类型,尽管 PHP 具有面向对象的特性,但除了过度使用数组数据类型之外,它往往别无选择。如果您想了解更多内容,请查看 PHP 手册中有关迭代器的部分。
Public, protected, private, and static properties of classes all work roughly as expected.
类的公共、受保护、私有和静态属性都大致按预期工作。