Ask SO: Good PHP code to read? - php

It's quite easy for me to study html/css, as it's openly available.
But I can't do the same with PHP/AJAX (and MySQL). I'm trying to learn both, but there's not much good code to find.
Any tips where I can find some (to read and to use)?

Read code to learn? I generally start writing code to learn it after the end of the tutorial. Go to http://www.w3schools.com/ that contain tutorial, sample code and a practice tool for you to write code and test it online.

Try all the tutorials you can find. After you feel like you cannot learn anything from them try and build an application.
You could also get a look at some php frameworks

Read Wordpress, CodeIgniter and CakePHP. Those are very well written.

The best thing to do in my eyes (its how I learned a huge amount intially) is to have a specific task you want to achieve, then break that task into logical steps and try to find information/tutorials to tackle each one. Then use google to pinpoint targetted tutorials (seriously, google is a heck of a resource, dont put it down, its basically an online library).
So, say you wanted a way of uploading a file into a database using HTML, PHP and mySQL - that is your task. The next thing would just be to search for say, 'PHP mySQL file upload tutorial how to'.
Frameworks are great tools for developing your applications, however I would tend to say (personal opinion) that at least initially, it is better to learn straight PHP, the ins and outs, the basic functions and uses. This will help give you the knowledge of how to apply/develop with a framework better as you will understand the advantages it offers relative to straight PHP and therefore how to utilise the framework more efficiently.

Related

How to write resuable php framework, code?

Hello there I am new to php and want to learn to write reusable php code, a framework to build CMS for the sites that I am going to create.
From where should I learn such approach. If Possible please share some of the links of tutorials pdfs anything that can help me.
Try to re-invent the wheel. Read the TinyMVC source code and try building a framework similar to it. Then you can read and re-invent bigger framework. (I did this with Zend Framework, if you ask)
Of course, you should do this for learning purpose only. For your real projects, I suggest you to use existing PHP frameworks out there.
Like Michael said, don't re-invent the wheel.
You can choose from many frameworks when it comes to PHP. You might also consider using Ruby on Rails. In my experience, all 'good' PHP frameworks are just Ruby on Rails wannabes and bad at that too. But saying that is just begging for a flame war, so lets all pretend I didn't just say that.
---- on topic ----
You say that you are a beginning PHP-er and want to learn to 1. build reusable code and 2. build a CMS using 1.
An important practice of writing reusable code to me is that you split up functionality of a library you make up in as many parts as you can. To take a recent example of a digital store I am building, I would have a function that clears my shopping cart, one that clears the register one that resets the fields, etc. By splitting the code up in these different functions I was able to call some of them when a transaction was complete (with the addition of saving the transaction) and I when a search was completed (just reset the fields). Think of it like a chest of tools where you can either have screwdrivers attached to all the other tools, or you could just have 1 screwdriver. And this is just one aspect of it.
#Galwegian posted a good answer about more elements to reusable code on SO: How do you make code reusable? as well.
Learning how to build a CMS is a pretty broad question. If you were doing it just for the fun of it or just so to learn how to write better PHP; look around at what other CMSes are doing and wonder how they solved certain things, what makes them work or not and try to figure out how you can build that too.
If you want to build websites professionally; please don't create your own CMS. You will hate yourself for it later. Go for something with a wide user base and many tutorials, like drupal or wordpress. When building your own CMS you'll just be stuck wasting time finding solutions to problems that were already solved a lot better by others.
-- half topic---
I saw this PHP course coming by, it isn't free though, but it should get you started. Nettuts is a good resource for tutorials and alike either way, check it out.
http://net.tutsplus.com/tutorials/php/php-fundamentals-new-premium-course/
Why not just use one of the many many already existing PHP CMS frameworks out there?? Drupal, for example, is easy to extend. Don't re-invent the wheel.
Here is an example of Jeffery Way's tiny MVC framework which you could play with http://net.tutsplus.com/tutorials/php/create-your-first-tiny-mvc-boilerplate-with-php/

A noob needs some quidance on php, mysql programming

I am working on a (dating, social) website. Basically, it's database system in which registered users can search, update their own data, send messages to each other, upload photos. I am a noob in web programming. I am writting the code (html, css, php, sql queries) line by line. It was okay when I had only 3-4 pages (registration, profile update, search, contact list), but now it's getting more complex (15-20 pages) and I find it hard to keep track of functions, variables, css ids and so on. Moreover I am asked to add more features (user blogs, forums) soon that will make it even more complicated...
I would like to get some guidance from professionals to speed up the development process. I have a half-ready system with my own database tables, php files, functions. And I have some experience in coding. I work with Eclipse, but I use it only as a text editor.
So, what's next? What development tools would you use to code such a system?
Thanks.
I have some general advice that I think will help someone starting out in any programming field
Find other people doing the same kind of thing and talk to them about how they do it. They don't all have to be experts as you can still learn from other beginners, but don't only talk to beginners or you will just learn bad habits :)
Use other people's code wherever you can. Frameworks, libraries, examples you found online. If it is a framework or library that lots of people use then go ahead and use it however you like. If its an example you found on a blog or something then make sure you really understand what it does because a lot of the stuff out there is rubbish!
Eclipse is fine as a text editor, as are many others. Changing text editor will not make your code better or easier to manage. Having development tools that you like using can make the whole process much more pleasant but it is not as important as you might guess from reading around the 'net. Spending two weeks learning a new coding environment doesn't help you get your project under control.
Try to make sure you pass as much of the Joel Test as possible, even in a team of one. You should be able to answer yes to at least questions 1, 4, 5, 6, 7 and 10.
OOP and i would advice to take a look at other open source social websites/engines to see if,
1) migration possible (since yours is half-ready, according to you)
2) reuse their framework/ underlying models possible.
and since you will add more features to your existing one, i recommend you to take a look at below:
FYI
[1] Social Engine.Net http://www.socialengine.net/
[2] Social Network Engine Comparison (by Wiki)
As for coding IDE / development tools/techniques, you will increase your productivity if your environment is php integrated(on the fly function/method lookup).
I would strongly suggest using a PHP framework like Zend, Symphony etc. than simply writing PHP files. It will help you use something called an ORM for database queries which helps you port from different database servers like Oracle, MySQL etc. Apart from that, your question needs a very detailed answer. Try looking up some of the software development practices and tools that assist in them (e.g. trac, svn etc.) I have found these to help me be organized and stick to a schedule.
And how many users are you looking to serve ? Think about the scalability of your design.

What language to use - simple form + MySQL + admin page

Apologies if this is not the right place to ask but here goes...
What should I use to create a simple web application for our website?
I'm the IT guy for a small non-profit. On our website we have a page with a large form in which users can fill out information on an application form. The information gets sent to a MySQL db. This data can then be accessed and edited by a couple of members of staff behind a logged-in part of the website. There's only two dbs being used: the main one with all the data and the one used for login details for the web app. It's all done with PHP and functionally is fairly simple - just a form with lots of fields to collect data and a basic secure "manager" page to do a bit of stuff with that data.
The problem is that we need to make changes to the app, and to extend its functionality quite a lot. I have a little bit of experience playing around with PHP but I've taken one look at the code used and decided it'll take too long to decipher it and see what's happening where. The code is uncommented and a bit of a mess.
I'm starting to think that rather than investing time to relearn what little I knew about PHP and untangle all the code, I might invest the time in learning another language and/or framework to get this done. I want the resulting web app to be a lot easier to maintain in future by me or anyone else who comes along and has to make a change.
Would you recommend using Django/Python for a project like this? Zend/PHP? Just PHP and notepad? I want the app to be done fairly quickly so the less steep the learning curve the better. Many thanks for you time.
Another good PHP framework is CodeIgniter.
They also have a good webcast that outlines how to begin with the framework as well as create a simple blog [tutorial].
I would recommend a PHP framework, such as CakePHP. Spend 20 minutes of your time and follow their blog tutorial.
Using PHP alone can be tricky, especially when you will have to deal with security issues.
From what I can see you have asked 2 questions.
1. Should you maintain and extend the current code base or do a rewrite?
Rewrites always take longer than you think. And even if you do the rewrite to avoid learning the codebase you would still need to learn the current codebase to ensure you capture the current functionality before adding any new features in the rewritten codebase.
I would keep the current codebase and maybe do some refactoring as you add features.
What should language and frameworks should you use?
I would stick with PHP, CakePHP is a solid framework and so is Zend. I would read up on both and do a couple of tutorials and make your decision.
As Anax states, I'd suggest that you look to use PHP. If code is already implemented then you have a start.
PHP isn't hard to re/learn. I know some don't like it, others like it but simple fact, either way it isn't hard to learn. But, more importantly consider the following:
You obviously have access to the hardware stack required to use PHP and MySQL. Introducing a new language/technology may cause unforeseen issues with getting a production site set up.
Now this doesn't mean that you can't write certain components in other languages. You could, if you were so inclined. But you probably shouldn't be so fast to get rid of PHP just to learn another language. Do you have important (technical) compelling reasons to use something other than PHP?
If you are mostly concerned about the spaghetti code in place, you can get that anywhere -- even new development. Better to re-factor the code and fix and add to what you have than start from scratch.
I can offer some general considerations:
Whatever language you choose, get a good IDE for it. Having automatic syntax validation and code completion helps a lot if you're a beginner. Don't use plain text editors.
Teaching yourself how to code well will make for a very frustrating experience unless you have a lot of patience. This generally comes from being really sure that you want to do it.
Be very conservative in your time estimates. Having many setbacks is guaranteed if you're a beginner.
Start out by focusing on reading about writing code, not focusing on writing code. If you're learning Python, read the official tutorial first. The same goes for PHP.

Writing Clean and Efficient PHP Code

I have been looking around online and I cant seem to find an article on this that isn't totally outdated. Does anyone have any good articles that give some good advice? What I have read so far is good, and a bit helpful, but I want to have modern code examples, not ones from 2002.
I have coded an extensive PHP/MySQL program and I am trying to make it better now, any suggestions?
EDIT:
People are suggesting frameworks, and I appreciate it but I am looking for coding tips for raw PHP Coding. The whole application is already done and it would be very lengthy to recode the entire thing in a framework.
I will be checking those out for future projects though. Does anyone know or have any tips for Writing Clean and Efficient PHP Code?
Use Smarty
Write to patterns as much as possible.
Edit for the Question Edit
Clean and efficient PHP isn't much different than any other language. Practice good Object oriented principles. Focus on Encapsulation, Separation, and Polymorphism. Refactor often, and Utilize design patterns as much as possible.
There are many sites that focus on these principles. Some use Java, C++, etc. to demonstrate the principles, but it shouldn't be too difficult to convert them over to php.
I'd take a look at the Zend Framework. It is a great framework that encourages PHP developers to use better practices like MVC, OOP etc. If you are not used to this paradigm, I have to say that it will seem daunting at first, but if want to continue with your PHP development I'd suggest downloading it, and start the "Getting Started" tutorial.
There are other frameworks, CMS tools that you can download, but the Zend framework offers are very raw approach to building great PHP applications.
Acorn
PHP Classes helped me a lot (when I was coding PHP). Reading better code usually helps improve my own.
If you haven't already, I suggest making your PHP code object-oriented. It promotes reuse and makes code a lot more readable. Here's a link to show how useful it can be for reducing lines of code etc.
Use CakePHP. It's an excellent MVC framework. Steep learning curve, especially if you've not used MVC before, but the tradeoff is certainly worth it.
Like folks said, frameworks are the way to organize your code best. Many support Model/View/Controller; many are object-oriented already. Try to stick with a PHP5-specific framework (the code will be much cleaner; PHP4 did not support OOP fully). Try to go with the simplest possible templating engine. One PHP5 framework I like that hasn't been recommended yet is called QCubed.
One thing in your post worried me, though - you're saying that you already have the application written, and are now trying to make it "better". If that's the case, trying to switch over to a framework now may be a HUGE (literally huge) task. If this is an application you expect to be in production for 3-5 years, I'd consider taking a plunge; I'd then try to take little pieces of the application and integrate them into that framework environment. Doing it wholesale is a recipe for pain.
For clean PHP code you can use PHP CodeSniffer (search for CodeSniffer on http://pear.php.net since direct links don't work) it's an automatic code style checker. You can define your own codestyle rules and then check the code.
You can even script it, so your code gets checked before SVN commit.

Is this online tutorial a good starting point for creating a PHP5 framework for learning reasons?

I have found this tutorial online
http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/
I have created myself a simple sort of framework, but I have not been too knowledgeable whether I have followed best practices and the like... which I really would like to do.
I am looking at making my framework more unified by the MVC pattern. It seems I have sort of followed the principles, but I won't be happy until I can say my framework follows the design verbatim (and can, when it needs to be, easily picked up by another developer).
My question is, is this tutorial online a good place to start? I am using PHP5 with Apache.
Please note, I am not looking for the 'Why reinvent the wheel' type of answers, I'm doing this for fun and as a learning experience.
Alex, I must say I haven't read the article and please don't take this the wrong way, but I think the best way to learn about shortcomings of your own framework or to find out if it meets certain standards is to open it up - make it open source - and let other developers take part in it. They will be pretty quick to complain or point out flaws.
Even if your only goal in creating this framework is to learn, I think you can benefit from a code review. And what better way to get a code review than by inviting other people to join you in your "quest".
This way you should also have the chance to challenge the others code and maybe ask questions to find out why they write code a certain way.
I hope you'll find this at least a little bit helpful ;)
P.s. I'm going to take a look at that link now..
I haven't read the article you referenced, but one way to find out if your framework is useful is to try using it in a different way than we did before.
You may want to experiment with when to use classes in PHP and when not to, as it isn't one size fits all.
You may want to try to not have your business logic tied up in the same php file as your html, but keep them separate if you want to follow good MVC practices.
Good luck. :)

Categories