Re: Structured PHP studying

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



David McGlone wrote:
> On Saturday 24 April 2010 10:46:33 Nathan Rixham wrote:
>> Aside: in every project i do, and at all times i have a test.php where I
>> literally just try out ways of doing things and snippets of code, once
>> I'm done i stick an exit; before it then do the next snippet above it,
>> this way i always have one nice file of snippets in every project to
>> refer back to - they often come in handy for helping on this list
>> actually, and in fact sometimes are from peoples code on this list!
> 
> Great Idea! I probably would have never thought of this. How do you include 
> these snippets into your actual work to see if they work? Do you just run the 
> file or make functions and just include the function in the code you are 
> working on?

typically I use it for handling little scenarios, will try out a couple
of approaches and then whichever is best I'll incorporate in to my
script/class/method/function and just leave the test code in the
test.php file for future reference.

each little snippet of code we make has one task to do, normally given
the values (x,y,z) create something new, perform some calculation or
take some action and return the status - regardless of how we wrap that
up in the end, and where we get our values from, the code "in the
middle" will typically be the same; so if I write:

$tests = array( 'nathan', 'nath.an', 'nat:@han$%^&' );
foreach( $tests as $test ) {
   echo trim( preg_replace( '/[^a-z.]/im' , '' , $test ));
}

in my test.php to figure out how to strip all non alpha chars, then when
I'm happy with the results, I'll simply write a function/method to do
that task in my real app.

function( $string )
{
  return trim( preg_replace( '/[^a-z.]/im' , '' , $string ));
}

to run the test.php I just use my IDE, but you could easily just php -f
test.php

the main point of doing this is that after I've tried one approach and
am ready to try the next i stick and exit; before it, this way it won't
run, but its catalogued there - kinda like taking notes back when we
were in school and "show how you worked it out", always useful to look
back on.

>> Coding Standards
>> One of the biggest factors of feeling your code isn't good enough, is by
>> not using any coding standards,
> 
> This is absolutely what I have learned in the past couple months. I bought 2 
> books called Beginning PHP and MySQL E-Commerce From Novice to Professional 
> 1st and 2nd edition by Cristian Darie and these 2 books build a T-Shirt web 
> site from start to finish, and I realized with these books just how important 
> organizing your code and following standards is.
> 
>> this is pretty simple stuff but makes
>> your life a lot easier, and code a lot cleaner - even crap broken code
>> comes across a lot better when it's nicely formatted lol. A good IDE can
>> help you here, as for which coding standards that's up to you, but you
>> can check you are adhering to them by using "php code sniffer" -
>> personally I opt for a slightly modified version of zend coding
>> standards with the odd bit of extra spacing around params etc.
> 
> It's sad as long as I've been pushing myself to learn PHP, and studying 
> anything I can get my hands on, I've never heard of "php code sniffer".

http://framework.zend.com/manual/en/coding-standard.html
http://pear.php.net/package/PHP_CodeSniffer/redirected

netbeans also has support and this for eclipse http://www.phpsrc.org/
(which i haven't used yet) - personally I use phpUnderControl for
automated building and have ant tasks set up to run all my code through
php lint check, php code sniffer, php doc and to run all unit tests,
then if it's okay it is automatically deployed out to my tests sites,
and if not informs me that the build (code) is broken.

> On a side note: the trick is to make the code look so darn gibberish, that it 
> makes whoever had written it look like a genius ;-)

actually mate that the complete opposite of what you want, and a sign of
very very poor coding indeed ;) you want the code to be as simple and as
clean as possible, minimal whilst crystal clear - small short clean
functions that do one task properly and only do that one task - that's
the sign of a good coder and one who has mastered (or is mastering) the
art of programming.

incidentally "the art of computer programming" is exceptionally good if
you have the time and money:
http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming
http://www.amazon.com/Art-Computer-Programming-Volumes-Boxed/dp/0201485419


> Thank you Nathan! I have bookmarked all the sites and will be reading all of 
> them. Your words have inspired me to give it my all once again.

brilliant! :D

Best,

Nathan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux