Tizag.com Webmaster Tutorials - A collection of webmaster tutorials from HTML to PHP.

Monday, January 25, 2010

Calling Methods of PHP Class Globally

<?php
class saveData{
private $filelocation, $dataSaved;

public function __construct($file, $data){
$this->filelocation = $file;
$this->dataSaved = $data;

if(!$data || strlen($data)>=256){
throw new Exception("Text shold not be more than 256 characters.");
}
if(!file_exists($file)){
throw new Exception("File missing in the location specified.");
}
}


public function saveData(){
if(!$fp = fopen($this->filelocation, 'a+')){
throw new Exception("Error in opeining the target file.");
}
if(!fwrite($fp, $this->dataSaved)){
throw new Exception("Error in writing in the target file.");
}
fclose($fp);
}

public function __toString(){
return "Data Successfully Saved.";
}
}
?>

<html>
<head>
<title>Save Data | Calling Methods of a Class Globally</title>
</head>
<body>
<form name="frm1">
<textarea name="typeValue" cols="20" rows="8"/></textarea>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
$dataSaver = new saveData("document.txt", $_REQUEST["typeValue"] );
$dataSaver->saveData();
echo $dataSaver;
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}


}
}
?>
</body>
</html>

Saturday, January 23, 2010

Simple example with __toString() - PHP

__toString()



__toString() is a magic function, and allows you to set a string value for the object that will be used if the object is ever used as a string.

<?php
class TotalPrice{
protected $price, $quantity, $total;

public function __construct($productPrice, $productQuantity){
$this->price = $productPrice;
$this->quantity = $productQuantity;
$this->calculatePrice();
}

protected function calculatePrice(){
$this->total = ($this->price * $this->quantity);
}

public function __toString(){
return "Total Price :: ".$this->total;
}

}

$totalPrice = new TotalPrice("2.00", "6");
echo $totalPrice;
?>

Wednesday, January 20, 2010

Exception Handling in PHP | try | throw | catch

Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".

Throw - This is how you trigger an exception. Each "throw" must have at least one "catch".

Catch - A "catch" block retrieves an exception and creates an object containing the exception information.

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
  • An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
  • Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
  • Exceptions can be thrown (or re-thrown) within a catch block.

refer http://www.tutorialspoint.com/php/php_error_handling.htm

A simple example on Exception Handling in PHP | try | throw | catch



<?php
function displayDetails($text){
if($text<=10){
throw new Exception("Value should be greater than 10.");
}else{
for($i=0; $i<$text; $i++){
echo $i."<br/>";
}
}
}
?>
<html>
<head>
<title>Check Try | Throw | Catch</title>
</head>
<body>
<form name="frm1">
<input type="text" name="typeValue" value="Type text and hit go"/>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
displayDetails($_REQUEST["typeValue"]);
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}


}
}
?>
</body>
</html>

Tuesday, January 19, 2010

Increase count in column while updating a field in MySQL

Increase count in column while updating a field in MySQL


UPDATE <tablename> SET <thiscolumn>=<thiscolumn>+1 Where id=500