Jump to content

OO in PHP


Sanjib Sinha

Recommended Posts

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?

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

Many thanks for your reply friends. I'm inspired and decided to move on.  :rtfm:

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?

Link to comment
Share on other sites

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? :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:

or

 

$file = "file.with.dot.txt";
$extension = explode('.',$file);
$extension = $extension[count($extension) - 1];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.