Tag Archives: class

The philosophy of object oriented programming

Standard

Programmatically today is good. I added user and membership tables using aspnet_regsql.exe in a database. After creating it I connected it through Provider class in web.config.
Next, I created roles programmatically and assign roles to users.
This is great. I wanted to do it for last two three days and failed. But the method is not very difficult at all.
You use a command line code like this:
aspnet_regsql.exe -d -E -A all
pressing enter the required classes(tables) will have added to the database. Now using the proper connection string you can get connected to the database.
The next steps are pieces of cakes as you drag web controls from Login toolbox. The default classes automatically attached and next thing you write down a class which inheriting from SqlProvider overrides the methods to create Roles and assign users to that roles.
Asp.Net ships with plenty of classes that you can use with your classes and create objects accordingly.
This is the greatness of Object Oriented Programming.
I find it Philosophical getting reflections of the real world. Don’t you?

Display Image In PHP and ASP.NET

Standard

In a nutshell it says same thing. When end user will load a page, few pictures will automatically opened up.
In PHP, a DirectoryItems class has been constructed and later in index.php page that class has been used to dispaly images.
First the code of DirectoryItems Class:

class DirectoryItems{
//data members
private $filearray = array();
private $replacechar;
private $directory;
//////////////////////////////////////////////////////////////////
//constructor
/////////////////////////////////////////////////////////////////
public function __construct($directory, $replacechar = "_"){
$this->directory = $directory;
$this->replacechar = $replacechar;
$d = "";
if(is_dir($directory)){
$d = opendir($directory) or die("Failed to open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}else{
//error
die("Must pass in a directory.");
}
}
//////////////////////////////////////////////////////////////////
//destructor
//////////////////////////////////////////////////////////////////
public function __destruct(){
unset($this->filearray);
}
//////////////////////////////////////////////////////////////////
//public fuctions
//////////////////////////////////////////////////////////////////
public function getDirectoryName(){
return $this->directory;
}
//////////////////////////////////////////////////////////////////
public function indexOrder(){
sort($this->filearray);
}
//////////////////////////////////////////////////////////////////
public function naturalCaseInsensitiveOrder(){
natcasesort($this->filearray);
}
//////////////////////////////////////////////////////////////////
//returns false if files are not all images of these types
//////////////////////////////////////////////////////////////////
public function checkAllImages(){
$bln = true;
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
$bln = false;
break;
}
}
return $bln;
}
//////////////////////////////////////////////////////////////////
//returns false if not all specified extension
//////////////////////////////////////////////////////////////////
public function checkAllSpecificType($extension){
$extension = strtolower($extension);
$bln = true;
$ext = "";
foreach ($this->filearray as $key => $value){
$ext = substr($key,(strpos($key, ".")+1));
$ext = strtolower($ext);
if($extension != $ext){
$bln = false;
break;
}
}
return $bln;
}
//////////////////////////////////////////////////////////////////
public function getCount(){
return count($this->filearray);
}
//////////////////////////////////////////////////////////////////
public function getFileArray(){
return $this->filearray;
}
//////////////////////////////////////////////////////////////////
//eliminate all elements from array except specified extension - Phase 2
/////////////////////////////////////////////////////////////////
public function filter($extension){
$extension = strtolower($extension);
foreach ($this->filearray as $key => $value)
{
$ext = substr($key,(strpos($key, ".")+1));
$ext = strtolower($ext);
if($ext != $extension){
unset($this->filearray[$key]);
}
}
}
//////////////////////////////////////////////////////////////////
//eliminate all elements from array except images - Phase 2
/////////////////////////////////////////////////////////////////
public function imagesOnly(){
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
unset($this->filearray[$key]);
}
}
}
//////////////////////////////////////////////////////////////////
//recreate array after filtering - Phase 2
/////////////////////////////////////////////////////////////////
public function removeFilter(){
unset($this->filearray);
$d = "";
$d = opendir($this->directory) or die("Couldn't open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$this->directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}
//////////////////////////////////////////////////////////////////
//private functions
/////////////////////////////////////////////////////////////////
private function createTitle($title){
//strip extension
$title = substr($title, 0, strrpos($title, "."));
//replace word separator
$title = str_replace($this->replacechar, " ", $title);
return $title;
}
}//end class

Next the index.php page where this class’s properties and methods has been used.

The index.php code where DirectoryItems class has een used

The page looks like this:

The index page with Images being displayed

Next day I’ll show the asp.net page where you can do the same thing using .NET class library.

OO in ASP.NET and PHP

Standard

I won’t spend much time on explaining Object Oriented Programming as there already million of words have been written by the experts and few of them are really worth reading (to name a few, I presently read Pter Lavin’s book on OO in PHP finding it simply brilliant.).
What I just try to do is very simple, find a simmilarity between creating ‘Class’ in ASP.NET and PHP and using access modifiers I set their properties from private to public.
First, let me show how Class being written in ASP.NET
Let me crate a class called Person

//creting class
class Person
{
private _name;
public Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
//crating instance
Person person1 = new Person();
person1.Name = “Person1’s Name”;
Response.Write(“Person1’s Name is ” + person1.Name);

Next in PHP I follow the same procedure

And the page looks like this:

The thing is, I tried to keep codes of both great dynamic web language almost similar. So that one can follow the simmilarity between those get and set properties that make private variables public so that you can ultimately access them in different class, in different situation.
Hope, curious people find it interesting.