PHP Classes

How to Have Better Ideas to Developer New PHP Classes in 5 Steps

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Have Better Id...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2023-09-06

Posted on: 2023-09-06

Viewers: 191 (September 2023 until December 2023)

Last month viewers: 3 (December 2023)

Categories: PHP Tutorials, PHP community

How often did you spend time thinking in a PHP class that makes something useful for your job project, personal skill, or even sharing your ideas with the community?

Well, read this article in which I will share some points to evaluate your PHP class ideas and explain each point.




Loaded Article

In this article you will learn below:

1. Find a problem

2. Plan the Class Code

3. Start to Develop the Code

4. Add Version and Author Information

5. Update Your Class Regularly

Conclusion

PHP Classes logo


1. Find a problem

This is our first step. Find something that can do useful things like:

  • Print a graphic with the population of a city.
  • Get a lot of data from a database an print it.
  • Calculate values to solve scientific problems.

Once you find your problem, study how it works, how many arguments it may have, the limits of their functions, where your project can be a good tool, or even you can ask a friend, your boss, or somebody if they need a program that does 'x' in 'y' site and given 'z' arguments.

For example, I developed my emoji converter class because I need it for my blog site, and I think sharing it here can help everyone who needs 'change words to image objects' in a Web page given 'a text' as an argument.

2. Plan the Class Code

Once you start developing a PHP class, you need to define if you need sessions or even cookies to keep data for future calls or store it for user management.

After this optional choice, you must define almost 75% of the variables to use, but one is required. In my opinion, this will look like this:

private $errors = array("FIRST ERROR", "SECOND ERROR", "THIRD", "ETC");
I always define printError($e) function to be private to be able to print errors where and when you need to.
 
As an additional tip, to use professional error messages, you can define something like:
private $lang; #this will be the language
private $errors = array("eng" => array("One", "Two", "Three"),
                        "esp" => array("Uno", "Dos", "Tres"));

The $lang variable value will be key to the $errors array, you can add as many languages as you need.

3. Start to Develop the Code

It's time to define functions once you define variables and custom errors.

Not all the functions will be public. Just make public all the functions that a user needs to call for this code to do his work, like getFormdata($data,...);

The protected and the private functions can be like: Check the user input, do complex operations, storing database data in a session or a class variable or something that can help to reduce execution time.

Divide your class in sections to make the code easier to read for you and other people that will use your class.

Once you finish your functions and adding variables (if you need more), you must test it in every case posible.

"Enemy divided is enemy defeated."

4. Add Version and Author Information

Once you have finished all the tests of your class, you can optionally comment your information about yourself, and I use this format:
  • Author: Daniel Alan Guerrero Matamoros
  • Version: 2.3 (you can use any number, chars, etc.)
  • Release date: Year-Month-Day (I don't use this, but can be useful)
  • Contact email: Itachi2826@hotmail.com (I don't use this for now)

Even knowing that adding this information is optional, this step is very important in case the users find bugs or unexpected errors. In this case, a user can email you asking for an upgrade or a fix for that bug or error.

5. Update Your Class Regularly

This last step is not required but can help you get great acceptance for your PHP class.

You can upgrade your class by adding new functions, new error messages, and variables, or you can even do specialized sub-classes that develop similar processes but support more arguments, are faster, etc.

Conclusion

Well, these are all the steps I follow to have good ideas, to have a nice class with sections understandable to all, and to share your information to implement and fix errors, etc..

Good luck doing classes and make sure you divide your problem.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Have Better Id...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)