Does a PHP Generator Framework or reference exist? - php

Back in the scripted ASP and ColdFusion days, I used to work on projects that would build code generators for ASP or Coldfusion, typically in C++, in order to be more object oriented in application design and not have developers writing scripted code, which often was called "spaghetti code" for it's size and complexity.
Since I've been coding asp.net since 2000, I haven't really had to deal with the issue at all, since the platform is not scripted.
I may be working on PHP projects in the future, and I was wondering if any code generators for PHP exist, or if any good references for building these generators exist.
It would be for the Linux platform, not targeted to Win2008 Servers running PHP.

Why not just build a proper app in PHP instead of going through the hassle? Recent PHP is fully object-oriented and lets you do some pretty decent stuff. There are even frameworks that help you do this kind of stuff.

I'm rather sceptical on the merits of code generation in the context of a dynamic language, such as PHP. You can generally use other kinds of abstraction to get the same results. Unlike a statically typed, compiled language, it is rather easy to use dynamic hooks such as __get and __set and reflection, to make generic abstract objects.

It's better that you learned good development practices in PHP than use code-generators. PHP might be a scripting language but it is quite potent, has a nice OO model, plenty of good frameworks and other open-source packages.
It's up to the developer to create well designed and factored code base, regardless of whether it's compiled at run-time or not.

Related

What are the relative advantages of various Python/PHP web frameworks (particularly for my project)?

I'm deciding on a web framework for an upcoming project, and I'd appreciate any advice. We've decided to use jQuery for the JavaScript, and are heavily leaning toward Python or PHP (more Python) for our server-side logic. I'm especially interested in web2py because of its jQuery integration.
About our project
Our project is to develop a security console for a complex
cybersecurity system operating within an organization's internal
network.
This console will be largely server-driven, as messages come in from the network and must be pushed by the server to the user.
The user will also be able to initiate security actions, the implementation for which will likely be in C++.
The interface we've planned will be relatively rich, and I want to leverage jQuery's power as much as possible.
We have some control over the browser environment we'll be running in (e.g., we don't have to worry about clients with JavaScript disabled).
Our site is likely to have only a few, long-lived client connections.
We are looking for software components with permissive licenses, though we're using some copyleft components (I see that web2py is LGPL while Django is BSD, so +1 to Django)
We have about a month to create a functional demo of our system, of which this interface is a small (but visible) part.
About us
We are two developers with about 5 years of programming experience, but little web development experience. I have several years of Python experience and a summers' worth of experience messing around with PHP. My coworker has some Python experience and has never touched PHP.
I used Django once back in 2008, and was frustrated by the file and code structure, which I found highly unintuitive. Perhaps this structure is inherent to the MVC model (I've had similar experiences with Django and CakePHP since), and I just need to bite the bullet and memorize it.
My Question
Given the information above, what are the relative advantages of the various Python/PHP web frameworks for our project? As mentioned above, I'm especially interested in web2py because of its jQuery integration, though Django's dominance is (once again) hard to ignore.
Thank you very much for your time!
Before deciding on a framework, you should first decide if you want to commit to a language you are unfamiliar with.
You said you've both got minimal PHP experience, so you have to weigh up the advantages here; Will the pros for going PHP (if any) out weigh the amount of time the developers will need to spend to retrain?
(Although depending on your background experience, PHP should be very easy to pick up.)
If you frame it like that, PHP would have to have a pretty convincing offering to give you. From what I'm seeing, specifically Django vs web2py, they both seem very close in functionality - which is good, but doesn't provide the "you must use x!" scenario you may be after.
However, If you will be bringing more people in later and feel finding people to work with web2py will be difficult, it may tip it to PHP. You said your self, Django's popularity (and BSD license) is hard to ignore, and it should make it easier to find people for later expansion.
If it were me, in your shoes, I would go with web2py. Assuming the development team will continue to be Python focused for the foreseeable future.
Python vs PHP: Python
With python, you can always write wrappers for C code so you won't have to mess with starting other processes and passing args to them. That might be useful for your security functions.
Web2py will allow you to easily write a webservice for this too, to more easily integrate the C portions with the web-site infrastructure.
If you already prefer python, I would go with that. If you need to bring on web-developers later that are trained in PHP, teach them Python. It won't take long, and I'm sure they'll appreciate it in the long run. Plus, moving from a PHP MVC framework to web2py or even django would make things easier. I've used CodeIgniter for PHP and find that web2py was so much simpler and easy to understand.
Also as for the directory structure, django is not actually true MVC -- it's MTV (model, template, view).
I find web2py's organization a little more straight-forward. But yes, either way it can seem strange at first. I would say YES, you should bite the bullet and use MVC.
In web2py, the "view" is html markup with the ability to write raw python code. The controller extracts data from the model (database), attaches any needed files (css/js etc) and the model of course simply defines the structure of the data and allows you to access it in an OO way.
Lastly, I wouldn't tip my hat in favor of web2py just because of jQuery integration. It does use it, and a some of the built-in framework stuff (like response.flash/session.flash, the LOAD function that loads a page or data from another controller) rely on jQuery, but using it in another framework only means you have to write an include statement (e.g. ).
But, the way it allows/forces you to focus on development is what takes the cake for me.
I've been using Django as part of my work for a couple years now and truly enjoy it when I can make it work. Unfortunately, and maybe it's just me, but I end up spending hours working on configuration every time I start a new server, or try to make it work in a development IDE.
It's relatively simple to start a new project and start coding. But there are all sorts of little catches that keep things from working if you deviate from the norm. Things like if you want your django project to serve from a subdirectory like example.com/django. The information is out there to make it work. But it's hard to find.
I can't tell you if web2py has those same problems or not. I only just learned about it from your question. It does look slick and simple though. I can tell you that after the hassles of getting the applications to serve properly from whatever server configuration I'm using, django is way more fun to program with than plain php. PHP frameworks may differ.
From my understanding the project not usual web application, you must need event driven program, web server in python.
Django won't help you here.You should look into Flask, Flask has inbuilt console too.http://werkzeug.pocoo.org/, you might need to use twisted, gevent,Flask jquery.
I would recommend asking about web2py on its mailing list -- you'll get lots of help and advice there. Regarding the LGPL license, note that it only applies to the framework, not your application -- you can license your application however you like, including closed source/commercial.
It sounds like you'll need some server push functionality, but if you'll really only need a few long-lived connections at a time, you might not need to bother with solutions like Twisted or gevent. If necessary, though, web2py does include this (requires Tornado, and works using WebSockets, though can be adapted to use other methods).

What's a good non-web application development environment for someone comfortable with PHP/Javascript/Mysql?

I would like to be able to write database driven applications (i.e. standalone apps that are not web based and don't require a browser or Apache server to run)
I have attempted to do this in Codegear C++ Builder in the past but even though my 'background' is in that (C++ OOP with Borland Builder) it is so far removed from doing the same sort of thing with PHP/mysql and other web technologies that I found that I couldn't get very far for a lot of effort getting it to work. It was a while ago now but I was using the built-in database engine that comes with Builder and I just found it frustrating and difficult.
In other words - Is there something out there that will allow me to make use of web based languages to write standalone applications (specifically PHP/Javascript/mysql)
You can stick with PHP if you want. There are QT bindings, GTK bindings, OSX/Cocoa bindings, and you can call Win32 functions. I don't know how stable all of those are, but you can do GUI in PHP as well as the command line stuff.
As for other languages... PHP is very C like. It started as basically a scripting wrapper around C (IIRC), which is why you have functions named after the C standard library (like strstr). C like languages will feel quite familiar.
I would think Python would probably be the closest to PHP. It's a scripting language, syntax is somewhat close, it has a ton of libraries, and is very well supported and commonly used. I'd imagine it would feel pretty familiar. Using indenting instead of brackets for blocks can throw some people, but it fit the way I already indented my code.
Ruby is quite popular, and is also a scripting language. I think it's farther away, syntax wise, than Python, but I've never really used it so I can't promise that. I know it has at least GTK bindings.
Perl has a lot of resources and bindings, but isn't as easy to read as PHP since you have to learn the special variables like #_. It was never really my cup of tea.
You do have the C/C++ stuff, and Java has it's large library. You may want to go that way since you say you've used C++ before. If you're on a Mac (or willing to use GNUStep) you could go with Objective-C/Cocoa. That's getting rather far from PHP syntax though.
All of these languages have database connections. You don't mention what platform you're on.
But for easy to work with, quick to pick up, works all sorts of places, and can definitely do GUIs... Python would be a good choice to look at.
You could always use PHP. It is a decent command-line / programing application. Other than that all I can say is that you knowledge of database access and storage will be helpful but by the end of the day you are going to need to learn a new language.
Most languages have libraries for database access. Just pick one that you like the feel of. It is also a good idea to choose one that is popular (for the community support) and free libraries are always nice. Also look for good documentation and one that is fairly standard.
A nice thing to know is that javascript and php syntax are very similar to many other languages. (Javascript looks almost identical to C and C++). Just read the main language tutorial then the database API tutorial and you should be good to go

Why phper does not care test?

In Java or Ruby, there are many talks about test/TDD/BDD. and there are many tests in Java/Ruby projects.
But I notice that, there are less tests in PHP project, and It seems like PHPer does not like to write tests, I just want to know the reason. thanks
I've used several applications or libraries, in a variety of languages, that did not include unit tests. This is not something isolated to the PHP community.
In fact, PHP has several testing frameworks available. Developers may either choose not to use them for whatever reason, or may be unaware of them. After all, a lot of PHP developers are self-taught with no formal training in computer sciences.
Honestly, most PHP developers I know that do use testing frameworks and other lifecycle tools tend to have been in the field for a long time, or come from another background like Java.
IMO there are multiple contributing factors:
It's a pretty new concept for PHPers. I don't use it myself. Most applications are written without contractual API design, hence code preceedes tests (in practice).
The test frameworks (PHPUnit and SimpleTest) are not natural.
PHP runs in a different environment than Ruby/Java. Moreover application parts are split into include scripts, which have different semantics from packages; sometimes locations variate.
PHP isn't as object-oriented to the core as other languages. Most code is procedural / mixed or hybrid.
I would go so far and ask about the merits of Unit-Testing for PHPs runtime environment. Selenium might be a better choice. But personally I'm looking for a simpler approach: simple (non-unit) test framework, similar to .phpt, should evaluate output/headers/errors/results

Best server-side language for MySQL and PHP

So I have access to a dedicated server and want to finally create my game - it's a browser based game, fairly simple. I know PHP and MySQL fairly well and would want to integrate with them on the front end.
The question is what is a good server-side language to use either as a constant process or with cron to interface with the MySQL database to do calculations?
Ideally my requirements are:
As short a learning curve as
possible.
Easiest access to MySQL as
possible.
As hard to shoot yourself
in the foot as possible.
I'd like to avoid C/C++ if possible for the above 'shoot yourself in the foot' problems. It'd also be nice to have a secondary language to use personally so I may take it beyond this limited scope.
Thanks in advance for any insights!
You could use commandline php with cron. The great advantage of that is that you can reuse libraries that you write for your game and you already know the language. I don't think it's performance is great though.
Besides that there are a ton of languages you might choose, Python and Java are popular.
There are two good options here:
PHP
PHP is a server side language, and if you design the frontend with some robust classes, you can reuse the classes for the command-line daemon. This allows you to have a more standardized application suite, which will be easier to modify in the future. PHP can access MySQL, and since you already know it, the learning curve will be minimal.
Python
Python is an excellent server side language for an application like this. It can talk to any SQL database with a standardized instruction set, called DB API 2.0. This means taht if at any point you want to change from MySQL to PostgresSQL, you simple change:
import my_mysql_library as sql
to:
import my_postgres_library as sql
And your application won't need any other code changes. Python also contains many libraries that might come in handy, and if optimized correctly, will be faster than PHP.
Conclusion
Personally, I would use Python, for the following reasons:
Learning Experience; Programming is always about learning. At any opportunity, use a language or tool you don't already know so that you can learn it.
Language Preference; After programming in both PHP and Python, I can honestly say that there is no situation where I would prefer to use PHP over Python. Not one.
Available Libraries; Python has a very robust community, and a lot of useful libraries and packages (such as NumPy) that make things much easier for you.
With both languages you could have them run intermittently via a cron job, or you could have them run as a Daemon (which is as easy as running the script(s) while piping your output to a black hole, such as /dev/null)
Another great option for your server side scripting is perl. It has a bit of a learning curve at the start but becomes a very fast language to script in.
A great resource is http://perldoc.perl.org/

Is there a Dependency Injection framework for PHP4?

I'm stuck on a PHP 4 server, and I would like to start moving an old legacy project to modern Design Patterns, including Dependency Injection. Are there any dependency injection frameworks that will work with PHP 4?
Most dependency injection frameworks use reflection to determine dependencies. Since PHP4 doesn't have typehints, you can't really do this. Experiments have been made with using config files - Some times embedded in comments in the code (Often called annotations). While this works, I find it a bit clunky. In my opinion, you're better off using PHP's dynamic nature to your advantage, than to try and apply statically typed solutions to it. You can get a long way with hand-crafted factories. See for example this post on how.
i found this (drip), but it looks like it hasn't been updated in a few years.
I don't think a dependency injection framework will really work on PHP because of the way object-oriented programs are structured in it. First of all, it's not like C# or Java wherein the binaries are already there and you just have to find a way to instantiate this object and inject it into another. PHP has to load the class files and interpret them before it can use them. So if you have deep inheritance hierarchies with PHP I don't think that's a good idea.
Given that PHP is a scripting language, it's best to leverage it as that -- a scripting language. Which means, I would just make use of simple factory or builder methods to do something similar to dependency injection. I wouldn't burden it with a DI framework that will only add to the stuff the PHP runtime has to process for every web request (unless you do opcode caching, but there will still be overhead that's not incurred by web platforms for Java and .NET). If I have to change the objects that will be injected into objects or how they are created, it would be a simple task to just edit the script that contains the factory/builder methods. No need to recompile there anyway. So I have flexibility and I have a lightweight architecture that's suitable for the PHP way of doing things.

Categories