Sanjib Sinha Posted April 16, 2010 Share Posted April 16, 2010 I'm from C# background, recently started learning Object Oriented PHP and found it extremely interesting. As I have little knowledge about the procedural parts, specially the PHP in-built functions, will it affect my learning or should I start from the classic, procedural way? Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/ Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Procedural would be C# without classes. You can always program in OO and not procedural. You'll pick up as you move along. Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043128 Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 If you want a sturdy grounding for PHP in the future, i would start with OOP simply because its not hard to understand. There are a few concepts that confuse people but basically, it means to seperate as much as possible without adding redundant code. So you can reuse your code in any other project that needs that functionality. So if you start in OOP, you will start your Code Base. -CB- Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043143 Share on other sites More sharing options...
Sanjib Sinha Posted April 17, 2010 Author Share Posted April 17, 2010 Many thanks for your reply friends. I'm inspired and decided to move on. But I've few questions, to start with I'd like to give a DirectoryItems Class codes where few in-built functions are used. First the codes: <?php class DirectoryItems{ //data members var $filearray = array(); //////////////////////////////////////////////////////////////////// //constructor //////////////////////////////////////////////////////////////////// function DirectoryItems($directory){ $d = ''; if(is_dir($directory)) { $d = opendir($directory) or die("Couldn't open directory."); while(false !== ($f=readdir($d))) { if(is_file("$directory/$f")) { $this->filearray[]=$f; } } closedir($d); }else{ //error die('Must pass in a directory.'); } } //////////////////////////////////////////////////////////////////// //public functions //////////////////////////////////////////////////////////////////// function indexOrder(){ sort($this->filearray); } //////////////////////////////////////////////////////////////////// function naturalCaseInsensitiveOrder(){ natcasesort($this->filearray); } //////////////////////////////////////////////////////////////////// function checkAllImages(){ $bln=true; $extension=''; $types= array('jpg', 'jpeg', 'gif', 'png'); foreach ($this->filearray as $value){ $extension = substr($value,(strpos($value, ".")+1)); $extension = strtolower($extension); if(!in_array($extension, $types)){ $bln = false; break; } } return $bln; } //////////////////////////////////////////////////////////////////// function getCount() { return count($this->filearray); } //////////////////////////////////////////////////////////////////// function getFileArray(){ return $this->filearray; } }//end class //////////////////////////////////////////////////////////////////// ?> Next in this code there are plenty of functions like substr() and strtolower() etc. From my C# programming background I guess what they mean, but where can I get to learn about them in detail? Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043654 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2010 Share Posted April 17, 2010 http://www.php.net/docs.php Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043655 Share on other sites More sharing options...
ignace Posted April 17, 2010 Share Posted April 17, 2010 PHP already has plenty of built-in classes have a look at http://php.net/manual/en/book.spl.php Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043668 Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 just a note, php has a great mod rewrite function, just type: php.net/any function here in yor address bar and it'll try to find the function for you, it will tell you everything u need to know - the comments help a lot too). -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1043821 Share on other sites More sharing options...
Ken2k7 Posted April 19, 2010 Share Posted April 19, 2010 The code is nice. I do have a suggestion though. For this line: $extension = substr($value,(strpos($value, ".")+1)); strpos gets the first occurrence of the string you're finding. Now, it's not at all unheard of for people to have dots in their filename, such as "directory.class.php" or "directory.inc.php". That line would get the wrong "." for the extension. May I suggest you read up on strrpos? Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1044402 Share on other sites More sharing options...
ignace Posted April 19, 2010 Share Posted April 19, 2010 The code is nice. I do have a suggestion though. For this line: $extension = substr($value,(strpos($value, ".")+1)); strpos gets the first occurrence of the string you're finding. Now, it's not at all unheard of for people to have dots in their filename, such as "directory.class.php" or "directory.inc.php". That line would get the wrong "." for the extension. May I suggest you read up on strrpos? Or maybe you should both have a good read of the manual and use pathinfo Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1044447 Share on other sites More sharing options...
dotMoe Posted April 19, 2010 Share Posted April 19, 2010 The code is nice. I do have a suggestion though. For this line: $extension = substr($value,(strpos($value, ".")+1)); As stated that is incorrect: proper way of fixing that error, i say is: pathinfo or $file = "file.with.dot.txt"; $extension = explode('.',$file); $extension = $extension[count($extension) - 1]; Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1044452 Share on other sites More sharing options...
ignace Posted April 19, 2010 Share Posted April 19, 2010 $file = "file.with.dot.txt"; $extension = explode('.',$file); $extension = $extension[count($extension) - 1]; WTF?!? Does no-one read the manual??? Proper way for this terrible thing would be: $file = "file.with.dot.txt"; $extension = end(explode('.',$file)); Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1044453 Share on other sites More sharing options...
dotMoe Posted April 19, 2010 Share Posted April 19, 2010 Thanks, haven't seen the end() function before. Quote Link to comment https://forums.phpfreaks.com/topic/198758-oo-in-php/#findComment-1044456 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.