Everyone is familiar with interfaces in java. but here iam trying to explain about interfaces in php.Iam not explaining here what interfaces means but iam expaining an example of code which is done using interface.Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public, this is the nature of an interface.
so here iam giving a small code which implements Interfaces in php......
Question is to create html and txt file according to the users choice.???
create an interface with following code.
<?php
interface interf{
public function fileopen();
public function filewrite();
public function fileclose();
public function setFileName($string);
public function settext($content);
}
?>
name interface as interf.php
Now we have to create two classes named Htmlwriter and textwriter which implements the interface:-
<?php
class Htmlwriter implements interf{
public $fileorg;
public $txt;
public $fp;
function __construct(){
$this->fileorg =null;// declaring variables publically
$this->txt=null;
$this->fp=null;
}
function setFileName($string){
$this->fileorg=$string;//setting the filename to public string
}
function settext($content){
$this->txt=$content;//setting the content text
}
function fileopen(){
$path="uploads/";
// if(is_dir($path)){
// $d=mkdir($path,0777,true);
// }
$name='uploads/'.$this->fileorg;//opening the filename that set publically with the setfilename function
$this->fp=fopen($name,"w");
}
function filewrite(){
fwrite($this->fp,$this->txt);//writing to the publically set file
return true;
}
function fileclose(){
fclose($this->fp);//closing the file
return true;
}
}
?>
name this file as Htmlwriter.php
now create another file called Textwriter.php
<?php
class Textwriter implements interf{
public $fileorg;
public $txt;
public $fp;
function __construct(){
$this->fileorg =null;
$this->txt=null;
$this->fp=null;
}
function setFileName($string){
$this->fileorg=$string;
}
function settext($content){
$this->txt=$content;
}
function fileopen(){
$path="uploads/";
// if(is_dir($path)){
// $d=mkdir($path,0777,true);
// }
$name='uploads/'.$this->fileorg;
$this->fp=fopen($name,"w");
}
function filewrite(){
fwrite($this->fp,$this->txt);
return true;
}
function fileclose(){
fclose($this->fp);
return true;
}
}
?>
save it.. save these interface and classes in libs folder in your main folder
don't forget to create a folder named uploads inside the main folder so that it can store the created html and text files.
now include the user interface file name it as index.php and paste code like this:-
<?php
include("autoload.php");
include("Htmlwriter.php");
include("Textwriter.php");
//include("interf.php");
// $db = new Factory();
// $db->connect();
//include("interf.php");
function SelectClass($type){// main factory method returns the object corresponding to the file want to create
if($type=='1'){
$rec=new Textwriter();
return($rec);
}
else
{
$rec=new Htmlwriter();
return($rec);
}
}
if(isset($_POST["saveForm"])){
$txt=$_POST["txt"];
$type=$_POST["type"];
$filename=$_POST["filename"];
$obj=SelectClass($type);
if($type==1){//checking which type of file to be created
$file=$filename.'.txt';
//$obj=new Textwriter();
}else{
$file=$filename.'.html';
//$obj=new Htmlwriter();
}
$obj->setFileName($file);//setting file name
$obj->fileopen();//opening the setted file
$obj->settext($content);//setting contents to be write to the file
$obj->filewrite();//writing to file
$obj->fileclose(); //closing file
if($obj){
echo "success";
} else
{
echo 'notsuccess';
}
}
?>
<h1> Form</h1>
<form action="index.php" method="post">
<div style="float:left">
<div style="width:200px;height:50px;float:left">File Type</div>
<div style="width:400px;height:50px;float:left"><select name="type">
<option value="1">Text</option>
<option value="2">Html</option>
</select></div>
</div>
<div style="float:left">
<div style="width:200px;height:100px;float:left">file name</div>
<div style="width:400px;height:100px;float:left"><input type="text" name="filename"/></div>
</div>
<div style="float:left">
<div style="width:200px;height:100px;float:left">Contents</div>
<div style="width:400px;height:100px;float:left"><textarea name="txt" cols="50" rows="20"></textarea></div>
</div>
<div style="width:200px;height:500px;float:left;padding:250px;"><input type="submit" name="saveForm" value="Save" title="Save Content" /></div>
<!--Content-->
</form>
Now it's time to run the code.
No comments:
Post a Comment