How to use php classes

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to use php classes

Post by Saman » Sun Jul 17, 2011 9:59 am

Elements in the classes can be declared public, private, or protected.
Public properties and methods can be accessed from any context.
Public Properties

Code: Select all

/**
* PHP5 Public Methods / Properties
*/

class Product {

	/**
	* Class Properties
	*/
	public $productPrice = 499;

	/**
	* Class Methods
	*/

	/**
	* Get Product Price
	*/
	public function getProductPrice() {
		return $this->productPrice;
	}

}

/**
* Class Object
*/

$product = new Product();
echo "Price: $" . $product->getProductPrice();
Output:
Price: $499

We can access public properties of a class directly as:
$product->productPrice;

Public Methods

Code: Select all

/**
* PHP5 Public Methods / Properties
*/

class Product {

	/**
	* Class Properties
	*/
	public $productPrice = 499;

	/**
	* Class Methods
	*/

	/**
	* Get Product Price
	*/
	public function getProductPrice() {
		return $this->productPrice;
	}

	/**
	* Get Product Price
	* Discount Manipulation
	*/
	public function getProductPrice2() {
		$discount = 100;
		return $this->productPrice - $discount;
	}
}

/**
* Class Object
*/
$product = new Product();
echo "Price: $" . $product->getProductPrice();
echo "<br />";
echo "Price: $" . $product->getProductPrice2();
Output:
Price: $499
Price: $399

Points to Remember
  • Public properties and methods can be accessed from any context
  • Be expert to access properties via methods instead of direct one
Chathura
Sergeant Major
Sergeant Major
Posts: 33
Joined: Mon Mar 08, 2010 2:42 pm

Re: How to use php classes

Post by Chathura » Wed Aug 31, 2011 3:40 pm

Thanks cool and clear. any help about constructor functions ?
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to use php classes

Post by Saman » Wed Aug 31, 2011 5:26 pm

Constructor
PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

Code: Select all

<?php
	class BaseClass {
		function __construct() {
			print "In BaseClass constructor\n";
		}
	}

	class SubClass extends BaseClass {
		function __construct() {
			parent::__construct();	// NOTE 1
			print "In SubClass constructor\n";
		}
	}

	$obj = new BaseClass();
	$obj = new SubClass();
?>
NOTE 1: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.

Destructor
PHP introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.

Code: Select all

<?php
	class MyDestructableClass {
		
		var $name;
		
		function __construct() {
			print "In constructor\n";
			$this->name = "MyDestructableClass";
		}

		function __destruct() {
			print "Destroying " . $this->name . "\n";
		}
	}

	$obj = new MyDestructableClass();
?>
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.

The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.
Chathura
Sergeant Major
Sergeant Major
Posts: 33
Joined: Mon Mar 08, 2010 2:42 pm

Re: How to use php classes

Post by Chathura » Wed Aug 31, 2011 5:52 pm

Excellent, Thanks :biggrin:
Post Reply

Return to “PHP & MySQL”