Diving into PHP: Day 15

Continuing with our OOP review, today, we’ll create a very simple “calculator” class that should give you a better idea of how abstracting your code to its own class can save you a great deal of time. We’ll also take a look at the helpful “__construct” method, as well as creating variables. Enjoy!

Day 15: Calculator Class

Download the Source Code



33

Comments
  • I have been waiting for this video. I saw all the episodes. Thanks jeff.

  • dave says:

    Just noticing that in all of your PHP files you never close the tag. That is just bad practice IMO, you should always close your tags!

    Other than that a good, simple tutorial.

  • Clemente G says:

    Nice tutorial. Its cool how all OOP languages are all pretty much the same.

  • Jeffrey says:

    @Dave – It’s not bad practice at all. If you’re working with a class file which is 100% PHP, it’s better practice NOT to close it.

  • Scott says:

    @Jeffrey

    Really? I always thought people didn’t close their class files (that are all PHP) because it was some form of short hand or just laziness. But it’s actually good practice not to? Good to know!! Time to update some of my files..

  • Jeffrey says:

    @Scott – Don’t worry about updating your files. It’s not a big deal either way.

  • prock says:

    awesome as usual keep it up!

  • JF Verville says:

    Good tutorial Jeff! Can’t wait to dive into more advanced PHP OOP techniques!

  • CgBaran Tuts says:

    I was waiting for this thanks for great series

  • Pikatzu says:

    Hey im new here. was wondering where the rest of the days are??

  • henry says:

    Nice, I have been waiting for this for a long time now! Glad to see your using Intype its such a great/ powerful editor really light too, also has many built in tab trigger(snippets).

  • DJ says:

    @jeff Just an observation — when you pub your jeff-way.com site you may want to empahsize the hyphen.. I made an error in assumption and went to “jeff.way.com” which, as you know, is NOT you. However, except for a substantially different “gimme your email” attitude, it is under construction and not at all clear that it is NOT you. I’m sure that many of your unsuspecting readers are unwittingly giving the guy their email addresses before they realize it is not you.

  • Johan Marklund says:

    Nice to hear your voice again jeff :P , really good tutorial, i have learnd many things from all these diving into php:D

  • Thanks for wonderful tutorial….

  • Jarod Taylor says:

    Hey Jeff, is there an easy way to get to a list of 1-15 on this series?

  • Alex says:

    Great tut Jeff!

    What are the most common uses of classes?

  • Jarryd says:

    I really should start watching these this weekend, in fact I am scheduling it in right now…between barbecues and parties and band practice :-)

    @Pikatzu You can find most of the ‘Diving Into PHP” screencasts here http://net.tutsplus.com/videos/screencasts/diving-into-php/

  • Clint Davis says:

    Hi Jeff,

    I still don’t understand the reason behind classes
    I know that a lot of applications use classes but im not sure why.

    I can understand when you mentioned the __construct ability.
    That makes scene for database.

    However apart from that why would you use it and not just separate functions?
    It just seams like lots of extra text to type for the ability just to group the functions?

    Clint.

  • nOuky says:

    Why would it be bad practice to close tags in classes and good practice not to close them???

  • haizone says:

    hay man
    i new in this site
    and i no speak english so good
    but your PHP video is amazing
    and i dont anserstend all the class stuff
    but today is my like day
    plzz continion all the this like this and alot of video OOP
    10XXXX

  • Jeffrey says:

    @nouky – I never said it was “bad practice”. It’s perfectly acceptable. But by removing the closing tag, you don’t have to worry about white space issues. Either method is fine.

  • Matt says:

    @nOuky,

    If you find it more comfortable to use end tags in classes, that is up to you.

    However, when you are working with pure PHP code in a particular file, leaving off the closing tag IS a good habit. If you leave it there, ANY blank space after the tag might be interpreted as a blank line of HTML and often triggers a PHP error message. This is not always true, but because of the possibility of errors, leaving off the last tag is a much safer habit to get into.

    Once again Jeff, you hit the nail of the head with this one. Great tut!

  • Matt says:

    Man Jeff, you beat me to it!

  • James says:

    Awesomeness!

  • Kayla says:

    Another great tutorial, thank you. :)

  • Clark says:

    Hey, great tut, but I was wondering if you were going to talk about MySQL security as mentioned in previous tutorials?

  • Meshach says:

    Thanks Jeffrey!

    I’m really understanding this OOP stuff thanks to you. :)

  • Skofus says:

    Awesome! Can’t wait for the next “episode” ^^,

  • jermaine says:

    I too would like to hear what you have to say about MYSQL secuirity issues as well, can’t wait for day 16.

  • Mark says:

    Awesome job ! Easy example and easy comment.

  • Amin says:

    @Jeffrey – When I press the TAB key in InType the bundle shortcuts doesn’t work.. I have to go to bundle and keep on clicking on each… any suggestions.?

  • Viney says:

    Great Screen Cast! Just wanted to know if you’re continuing this series?

  • Mario says:

    I’ve gone through your entire Diving into PHP series and it’s great! Thanks so much for your clear and concise explanations. I’ve gone through PHP books and other tutes and you’re are the best. You even managed to get me inspired to take on OOP again. thanks once more for taking the time out to make these!