Developers Archive for the 'php5 tutorial' Category

mysqli and object-oriented Approach

mysqli and object-oriented Approach Thursday, January 11th, 2007

The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above.
mysqli is designed with an object-oriented paradigm which allows the developers to work with an object-oriented interface. It is quite similar, although slightly more elegant (even for this old procedural-hacker) in that there is no need to pass the database handle for every function call, as the instantiated object takes care of that.

MySQL Improved Extension Allows two type of Approach
1. Procedural style and
2. Object oriented style

Procedural style :
—————–
Procedural style is the style we do with php4 and bellow.

<?php
$link = mysqli_connect(”localhost”, “my_user”, “my_password”);

$res = mysqli_query($link, “SELECT * FROM users”);

$value = mysqli_fetch_assoc($res);
?>

Object oriented style:
———————
Object oriented style will be like

<?php
$mysqli = new mysqli(”localhost”, “my_user”, “my_password”);

$result = $mysqli->query(”SELECT * FROM users”); /// Calling query using msqli object

$row = $result->fetch_assoc(); //// calling fetch_assoc using the result resource object

$mysqli->close();

?>

PHP Convert RGB from/to HTML hex color

PHP Convert RGB from/to HTML hex color Friday, January 5th, 2007
This article provides two functions for converting HTML color (like #AAED43) to three RGB values ($r = 170, $g = 237, $b = 67) and converting RGB values to HTML color.
First function, html2rgb recognizes HTML or CSS colors in format #(hex_red)(hex_green)(hex_blue), where hex_red, hex_green and hex_blue are 1 or 2-digit hex-representations of red, green or blue color components.

# character in the beginning can be omitted. Function returns array of three integers in range (0..255) or false when it fails to recognize color format.

source code: php

{ if ($color[0] == '#') $color = substr($color, 1); if (strlen($color) == 6) list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]); elseif (strlen($color) == 3) list($r, $g, $b) = array($color[0], $color[1], $color[2]); else return false; $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); return array($r, $g, $b); } ?>

Second function, rgb2html converts its arguments (r, g, b) to hexadecimal html-color string #RRGGBB Arguments are converted to integers and trimmed to 0..255 range. It is possible to call it with array argument rgb2html($array_of_three_ints) or specifying each component value separetly rgb2html($r, $g, $b).

source code: php

Fluent Interfaces in PHP5

Fluent Interfaces in PHP5 Friday, December 29th, 2006

In PHP 5 terms, a fluent interface to an object is one where the setter methods return an object handle. It is perhaps simplest to always return $this, however any object handle can be returned. Here’s a simple PHP class that demonstrates how a fluent interface is built:

<?php
class Fluent {
public function hello() {
echo ‘hello ‘;
return $this;
}

public function world() {
echo ‘world’;
return $this;
}
}
$fluent = new Fluent();
$fluent->hello()
->world();
?>

The code above will output hello world. As you can see, a fluent interface is quite easy to implement.
This buzzword fluent interfaces is a way of chaining methods of an object together. By having a method return a reference to the object itself, return $this; you chain methods together like this $this->methodOne()->methodTwo()->methodThree();. This can make your code easier to read, and that is the point of using fluent interfaces, making your code easier to read.

The first example is a snippet from a class in which the method makeNormal() is used to create an order in the system. The makeNormal() method is part of the order object.

<?php
private function makeNormal(Customer $customer) {
$o1 = new Order();
$customer->addOrder($o1);
$line1 = new OrderLine(6, Product::find('TAL'));
$o1->addLine($line1);
$line2 = new OrderLine(5, Product::find('HPK'));
$o1->addLine($line2);
$line3 = new OrderLine(3, Product::find('LGV'));
$o1->addLine($line3);
$line2->setSkippable(true);
$o1->setRush(true);
}
?>

Next, they show the fluent interface version of the code.

<?php
private function makeFluent(Customer $customer) {
$customer->newOrder()
->with(6, 'TAL')
->with(5, 'HPK')->skippable()
->with(3, 'LGV')
->priorityRush();
}
?>

As you can see, the second example is easier to read, assuming you understand that the with() method adds lines to the order. As stated earlier, this bit of magic is accomplished by the with(), skippable(), and priorityRush() methods retuning a reference to the object. ($this) That is the one and only secret to fluent interfaces.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.