DiscoBiscuit Posted February 10, 2009 Share Posted February 10, 2009 Hi everyone. I'm trying to make my first object oriented php site. I wanted to ask what you all recommend in terms of good practice. Should I place all of my classes in a single file like class_lib.php, or is it better to have lots of individual files? Also, is there a function which can check if any value exists within $_POST or $_GET? Using isset($_POST) always returns true. I hope I explained that ok. Any help would be greatly appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/ Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 isset($_POST['key']) No, it is better to include your class files separately, at least in my opinion, then you can have the class_lib.php have a function like "callClass($classname)" so you include the file only when needed, or just have it include all the classes (not sure if one is better than the other, but yea). In my line of thinking, only call the classes when they will be used, should save processing time. Again I am not sure if it will, that is just my view on it. Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759167 Share on other sites More sharing options...
trq Posted February 10, 2009 Share Posted February 10, 2009 Classes shoud indeed be kept in there own seperate files IMO. They can also be included automatically using __autoload if you use good naming conventions. Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759171 Share on other sites More sharing options...
gizmola Posted February 10, 2009 Share Posted February 10, 2009 The pear convention (also used by the Zend Framework) is to have each class in its own file with the same base name as the class. You capitalize the first letter of the directory and class names. It is also important to understand the php include path. Typically you place your classes in a directory structure that is in the include path. Let's assume that the include path contains among other thing: '/var/httpd/mysite/include/' So you decide to create your own library, named for your site: MYSITE. And let's say that you create a utility class called Timer. Then you would have a structure like this: /var/httpd/mysite/include/Mysite/Utility/ Since everything up to the include directory is in the php include path, you can ignore it as far as naming is concerned. All that's important is that PHP can find and load those classes. And in the Utility directory, you would create your new class file name timer.php. Your class should then be named: class Mysite_Utility_Timer { } An autoloader as simple as this will now work for your classes: function __autoload($class) { $filename = strreplace('_', DIRECTORY_SEPERATOR, $class) . '.php'; @require_once $filename; } Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759204 Share on other sites More sharing options...
The Little Guy Posted February 10, 2009 Share Posted February 10, 2009 isset (in my opinion) is best for checkboxes. I perfer empty $test = str_replace(" ",$_POST['input']); // Remove spaces for the test if(empty($test)){ echo '$test is empty'; }else{ echo '$test = '.$test; } Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759206 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 isset (in my opinion) is best for checkboxes. I perfer empty $test = str_replace(" ",$_POST['input']); // Remove spaces for the test if(empty($test)){ echo '$test is empty'; }else{ echo '$test = '.$test; } If you put on "E_WARNINGS" to display as your errors, you will get one if the page did not actually post a key called "input" Using isset, will ensure that there is a variable and will avoid getting e_warning messages sent to your log everytime the page is called. That and instead of using str_replace (which you are missing the replacement parameter), trim would be much better as it only trims out the whitespace. The input from the form could be valid with spaces. Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759230 Share on other sites More sharing options...
gizmola Posted February 10, 2009 Share Posted February 10, 2009 I glossed over your set question. Of course isset($_POST) returns true, because that array is created by PHP in a web environment, even if the array has no elements. In general I don't see the value of checking whether it has any elements, as usually you are looking for particular elements. One effective approach in that fashion is to call array_key_exists(). With that said isset() works fine for $_POST['myvar']. The problem is that often with html forms the elements exist (are set) but don't have a value, which is why others have advocated using empty(). Checkboxes are an exception, because in html, a checkbox causes it to exist. When a checkbox is not checked, it will not exist in the $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/144680-a-couple-of-questions/#findComment-759239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.