<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webcodez - The database of web programming tutorials &#187; functions</title>
	<atom:link href="http://www.webcodez.net/tag/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Sun, 14 Aug 2011 14:01:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>PHP Classes</title>
		<link>http://www.webcodez.net/php-mysql/php-classes/</link>
		<comments>http://www.webcodez.net/php-mysql/php-classes/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 19:04:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=700</guid>
		<description><![CDATA[Object Oriented Programming ( OOP ) is programming with use of classes and objects. A class consists of constants, variables and methods ( functions to alter the class’s properties ). An object can be created from a class and can be altered by calling the class’s functions. Why would I use classes &#38; objects? You [...]]]></description>
			<content:encoded><![CDATA[<p><strong>O</strong>bject <strong>O</strong>riented <strong>P</strong>rogramming ( <strong>OOP</strong> ) is programming with use of classes and objects. A class consists of constants, variables and methods ( functions to alter the class’s properties ). An object can be created from a class and can be altered by calling the class’s functions.</p>
<h3>Why would I use classes &amp; objects?</h3>
<p>You may be asking yourself: why would I use OOP? That question has one simple answer: for writing your codes fast, efficient and keeping them clear.</p>
<h3>Creating a class</h3>
<p>As mentioned before, a class consists of constants, variables and methods. There’s one default method which ‘creates the object’. It is automaticly executed when an object of the class is created. In other words: it can be used to ‘initialize’ the object ( its default properties e.g. ). This method is called the construction ( __construct ) method.</p>
<pre name="code" class="php">class ClassName {
	public function __construct( ... arguments ...  ) {

	}
}</pre>
<p>What you may have noticed is that there’s the keyword ‘<strong>public</strong>’ before the function. This indicates that the function may be used/called from anywhere ( also from outside the class ). There are 3 types of keywords that indicate how a function may be accessed:</p>
<ul>
<li><strong>Public</strong> May be accessed from anywhere: inside the class, outside the class or from a<br />
parent  class</li>
<li><strong>Protected</strong> May be accessed from inside the class OR from a parent class.</li>
<li><strong>Private</strong> May be accessed ONLY from inside the class.</li>
</ul>
<p>Alright, knowing that, let’s go back to the construction method. The construction method may or may not need certain information to create the object and its properties. These need to be given as arguments to the class ( or: to the construction method of the class ).</p>
<p><strong>For example:</strong> for creating a <em>Product</em> object, we may need the id, name and price of the product.</p>
<pre name="code" class="php">class Product {
private static $id, $name, $price;
	public function __construct( $product_id, $product_name, $product_price ) {

	}
}</pre>
<p>The construction method now asks for the id, name and price of the product upon creating an object of the class (a <em>Product</em> object).</p>
<h3>Using &amp;amp; Creating class variables and methods</h3>
<p>To access variables and methods of the current class we use:<br />
<strong>$this-&gt;variable_name </strong>or <strong>$this-&gt;function_name(… arguments …). </strong>In the example of the product class, we can now set the class variables equal to the ones given as arguments to the construction method:</p>
<pre name="code" class="php">class Product {
private static $id, $name, $price;
	public function __construct( $product_id, $product_name, $product_price ) {
		$this-&gt;id = $product_id;
		$this-&gt;name = $product_name;
		$this-&gt;price = $product_price;
	}
}</pre>
<p>Now we can create any other functions for the class ourselves, just the same way as the construction method was formed. For example a function to ‘buy’ the product could be created:</p>
<pre name="code" class="php">class Product {
private static $id, $name, $price;
	public function __construct( $product_id, $product_name, $product_price ) {
		$this-&gt;id = $product_id;
		$this-&gt;name = $product_name;
		$this-&gt;price = $product_price;
	}

	public function buy() {
		echo “Successfully bought the product ‘”.$this-&gt;name.”’ for $”.$this-&gt;price.”!”;
	}

}</pre>
<h3>How to create an object of a class?</h3>
<p>So far we know how to create a class, but not yet how to create an actual object of the class. It’s however not so hard to do. An object of a class is created using the keyword new followed by the name of the class and brackets which may contain arguments that are required for the class ( by the construction method of the class ). An object is usually created inside of a variable:</p>
<pre name="code" class="php">$myObject = new ClassName( … arguments … );</pre>
<p>We’ll continue with the Product class we’ve been creating. The construction method requires 3 variables to be given values to: product_id, product_name and product_price. These need to be given values to when we want to create an object ( product ) of the class. We do this by giving them as arguments.</p>
<pre name="code" class="php">
$product = new Product( 1, “Example Product”, 9.95 );
</pre>
<p>This will create an object of the class and set the variable id equal to 1, name equal to “Example Product” and price equal to 9.95. We’ve created a ‘product’! Now we may use any of the public variables and functions of the class. For example the buy function we made.</p>
<pre name="code" class="php">$product = new Product( 1, “Example Product”, 9.95 );
$product-&gt;buy();</pre>
<p>This will output:</p>
<blockquote><p>Successfully bought the product ‘Example Product’ for 9.95!</p></blockquote>
<p>Note: the script of the class we created needs to be included in the script that creates the object of the class and needs to be put BEFORE it, so that the class is recognized when it’s created. Usually the code of a class definition is put in a separate file ( e.g. ClassName.class.php ) and then included into the file that uses the class. For example, assuming we put our class code inside the file Product.class.php:</p>
<pre name="code" class="php">
&lt;?php
include(“Product.class.php”);
$product = new Product( 1, “Example Product”, 9.95 );
$product->buy();
?>
</pre>
<h3>Extending classes</h3>
<p>Classes can also be <strong>extended</strong>. When this happens, we create a new class based on an earlier created class. The new (parent) class may use any methods and variables of the earlier created class ( the ‘main’ class ).</p>
<pre name="code" class="php">class MainClass {

}

class ParentClass extends MainClass {

}</pre>
<p><strong>For example:</strong></p>
<pre name="code" class="php">&lt;?php
class Product {
protected $id, $name, $price; //protected: can be accessed from parent class too
	public function __construct() { }

	public function buy() {
		echo “Successfully bought the product ‘”.$this->name.”’ for $”.$this->price.”!”;
	}
}

class Game extends Product {
protected $rank;
	public function __construct($game_id, $game_name, $game_price, $game_rank) {
		//may use any variables from the parent class (‘Product’)
		$this->id = $game_id;
		$this->name = $game_name;
		$this->price = $game_price;
		//may also create and use its own, new variables
		$this->rank = $game_rank;
	}
}

$test_game = new Game(1, “Test Game”, 19.95, 3);
//when creating an object of the Game class, the methods of the ‘main’ class may be used
$test_game->buy();
?></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Functions</title>
		<link>http://www.webcodez.net/php-mysql/php-functions/</link>
		<comments>http://www.webcodez.net/php-mysql/php-functions/#comments</comments>
		<pubDate>Sat, 25 Dec 2010 16:56:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[return values]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=640</guid>
		<description><![CDATA[Functions are used for processes that need to be executed various times. They make it easier, faster and more efficient to get a certain process done. To create your own function, the following structure is used: function funcName ( ... arguments ... ) { ... function process code ... } You can give the function [...]]]></description>
			<content:encoded><![CDATA[<p>Functions are used for processes that need to be executed various times. They make it easier, faster and more efficient to get a certain process done. To create your own function, the following structure is used:</p>
<pre name="code" class="php">
function funcName ( ... arguments ... ) {
	... function process code ...
}
</pre>
<p>You can give the function any name you like as long as there’s not already an existing PHP function with the same name. The arguments of the function are variables where the function needs values for ( data that’s required for your function to run ). For example: a function that retrieves all data of a user would require the user’s ID to be given as argument to the function. Upon calling the function, these arguments should be given values to. A function is called by its name followed by values for the variables that need to be given as arguments to the specific function, between brackets.</p>
<pre name="code" class="php">
funcName( … arguments … );
</pre>
<p>The function’s return can be of any type ( boolean, String, integer, anything ). However, do notice that returning a value is different from echoing a value. When a value is returned, it can be catched by a variable. When a value is echoed, it automaticly is displayed on the webpage. For example, a function that returns a value can be compared:</p>
<pre name="code" class="php">
$num = 9;
if(sqrt($num) == 3) {
	echo “The number is 9.”;
}
</pre>
<p>The function sqrt is a premade function that returns the integer square root of the number given as argument to the function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

