简体   繁体   English

有没有一种简单的方法将C#类转换为PHP?

[英]Is there an easy way to convert C# classes to PHP?

I am used to writing C# Windows application. 我习惯于编写C#Windows应用程序。 However I have some free hosted PHP web space that I would like to make use of. 但是我有一些我想要使用的免费托管PHP Web空间。 I have a basic understanding of PHP but have never used it's object oriented capabilities. 我对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. 更新:除了基础知识之外,不依赖于.NET框架。 The main aim would be to restructure the class properties, variable enums, etc. The PHP will be hosted on a Linux server. 主要目标是重构类属性,变量枚举等.PHP将托管在Linux服务器上。

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. 另外,要注意集合类型,PHP尽管它具有OO功能,但往往没有其他方法可以过度使用数组数据类型。 Check out the sections on the PHP manual on iterators if you would like to see beyond this. 如果您希望超出此范围,请查看有关迭代器的PHP手册中的部分。

Public, protected, private, and static properties of classes all work roughly as expected. 类的公共属性,受保护属性,私有属性和静态属性都大致按预期工作。

如果C#类使用它,那么在PHP中复制.Net Framework就是一个很大的问题。

It is entirely possible to write a PHP application almost entirely in an object-oriented methodology. 几乎完全可以用面向对象的方法编写PHP应用程序。 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. 您将不得不编写一些过程代码来创建和启动您的第一个对象,但除此之外,还有许多面向对象的PHP MVC框架。 One that I would look at as an example is Code Igniter because it is a little lighter weight in my opinion. 我将把一个例子看作Code Igniter,因为在我看来它的重量稍微轻一些。

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. 我建议你这样做,因为我已经对VB和C ++进行了C#转换。 That was a pain but the result was worth it. 这是痛苦的,但结果是值得的。

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. 如果你真的想要,你可以用c ++逐步将应用程序重写为java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM