How t0 protect .htaccess

Standard

Here is the code which shows the trick to protect the file using .htaccess file.

Protect File using .htaccess

Copy
 # protect file
<Files personal_data.txt>
order allow,deny
deny from all
</Files>

Enable access for particular user

Copy
 # protect file
<Files personal_data.txt>
order allow,deny
deny from all
allow from 1.2.3.4
allow from 45.85.245.105
</Files>

So above code will enable access from 1.2.3.4 and 45.85.245.105 only.

Advertisements

Access modifier public private protected

Standard

Definition of Access Specifiers
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.

Why do we need Access specifiers
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.

Explanation of each access specifier

  1. Private
    2. Protected
    3. Public
  1. Private
    A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:

class Customer {

private $name;

public function setName($name) {

$this->name = $name;

}

public function getName() {

return $this->name;

}

}

$c = new Customer();

$c->setName(“Sunil Bhatia”);

echo $c->name; //error, $name cannot be accessed from outside the class

//$name can only be accessed from within the class

echo $c->getName(); //this works, as the methods of the class have access

//to the private data members or methods

In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.

  1. Public
    A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:

class Customer {

public $name;

public function setName($name) {

$this->name = $name;

}

public function getName() {

return $this->name;

}

}

$c = new Customer();

$c->setName(“Sunil Bhatia”);

echo $c->name;         // this will work as it is public.

$c->name = “New Name” ; // this does not give an error.

In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.

  1. Protected
    A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:

class Customer {

protected $name;

public function setName($name) {

$this->name = $name;

}

public function getName() {

return $this->name;

}

}

class DiscountCustomer extends Customer {

private $discount;

public function setData($name, $discount) {

$this->name = $name; //this is storing $name to the Customer

//class $name variable. This works

// as it is a protected variable

$this->discount = $discount;

}

}

$dc = new DiscountCustomer();

$dc->setData(“Sunil Bhatia”,10);

echo $dc->name; // this does not work as $name is protected and hence

// only available in Customer and DiscountCustomer class

In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.

You will learn more about inheritance later in this tutorial series. You should revisit this tutorial and read more on the protected section again when you understand inheritance better.

Important Note of Access Specifier in PHP5
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.

Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online.

abstract class and interface class

Standard

Interface
An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.

To extend from an Interface, keyword implements is used.

We can have a class extend from more than one Interface.

interface Storable {
function getContentsAsText();
}

class Document implements Storable {
public function getContentsAsText() {
return “This is Text of the Document\n”;
}
}

class Indexer {
public function readAndIndex(Storable $s) {
$textData = $s->getContentsAsText();
//do necessary logic to index
echo $textData;
}
}

$p = new Document();

$i = new Indexer();
$i->readAndIndex($p);

In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed.

abstract Class

An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.

Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

To define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName { }

Example of Abstract Class

abstract class Furniture {
private $height, width, length;

public function setData($h, $w, $l) {
$this->height = $h;
$this->width = $w;
$this->length = $l;
}

//this function is declared as abstract and hence the function
//body will have to be provided in the child class
public abstract function getPrice();

}

class BookShelf extends Furniture {

private $price;

public setData($h, $w, $l, $p) {
parent::setData($h, $w, $l);
$this->price = $p;
}

//this is the function body of the parent abstract method
public function getPrice() {
return $this->price;
}
}
In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().

Difference between Abstract Class and Interface
Abstract Classes

An abstract class can provide some functionality and leave the rest for derived class
The derived class may or may not override the concrete functions defined in base class
The child class extended from an abstract class should logically be related
Interface

An interface cannot contain any functionality. It only contains definitions of the methods
The derived class must provide code for all the methods defined in the interface
Completely different and non-related classes can be logically be grouped together using an interface