codescracker


php

PHP Data Abstraction



« Previous Tutorial Next Tutorial »


Data abstraction in PHP means any representation of data in which the implementation details are hidden or abstracted.

Data abstraction is used to hide the working style of an object, and to show information of an object in something understandable form.

PHP Data Abstraction Example

Let's take a simple example on data abstraction in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Data Abstraction Example in PHP</title>
</head>
<body>
<?php
	abstract class My_abstract_class
	{
		abstract protected function pre_sub_fix_function($parameter_v);
	}
	class My_concrete_class extends My_abstract_class
	{
		public function pre_sub_fix_function($parameter_v)
		{
			return "I just love to read {$parameter_v} on CodesCracker.";
		}
	}
	$obj = new My_concrete_class;
	echo $obj->pre_sub_fix_function("PHP")."<br/>";
	echo $obj->pre_sub_fix_function("HTML")."<br/>";
	echo $obj->pre_sub_fix_function("CSS")."<br/>";
	echo $obj->pre_sub_fix_function("Java")."<br/>";
	echo $obj->pre_sub_fix_function("C")."<br/>";
	echo $obj->pre_sub_fix_function("C++")."<br/>";
	echo $obj->pre_sub_fix_function("JavaScript")."<br/>";
?>
</body>
</html>

The sample output produced by the above abstraction example code in PHP is shown below:

data abstraction example php

Here is another example also demonstrating data abstraction in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Data Abstraction Example in PHP</title>
</head>
<body>
<?php
	abstract class My_Abstract_Class
	{
		abstract protected function gettingValueFunction();
		abstract protected function prefixValue($prefixParameterValue);
		public function displayingFunction()
		{
			echo $this->gettingValueFunction()."<br/>";
		}
	}
	class AnotherFirstAbstractClass extends My_Abstract_Class
	{
		protected function gettingValueFunction()
		{
			return "AnotherFirstAbstractClass";
		}
		public function prefixValue($prefixParameterValue)
		{
			return "{$prefixParameterValue}AnotherFirstAbstractClass";
		}
	}
	class AnotherSecondAbstractClass extends My_Abstract_Class
	{
		public function gettingValueFunction()
		{
			return "AnotherSecondAbstractClass";
		}
		public function prefixValue($prefixParameterValue)
		{
			return "{$prefixParameterValue}AnotherSecondAbstractClass";
		}
	}
	$objectFirst = new AnotherFirstAbstractClass;
	$objectFirst->displayingFunction();
	echo $objectFirst->prefixValue('PrefixedValue ');
	echo "<hr/>";
	$objectSecond = new AnotherSecondAbstractClass;
	$objectSecond->displayingFunction();
	echo $objectSecond->prefixValue('PrefixedValue ');
	echo "<hr/>";
?>
</body>
</html>

The above data abstraction example code in PHP will produce the output given below:

php data abstraction

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test