Archive for category Object Oriented PHP5

encode and decode

<?php

mysql_connect(“localhost”,”root”,””) or die(” Mysql not connected”);

mysql_select_db(“eduaid”) or die(“Database not selected “);

$row = mysql_query(“select * from captcha”) or die(“Query Problem”);

while($obj=mysql_fetch_array($row))
{
$arr[]=$obj;

}

$json= json_encode($arr,true);

print “<pre>”;
print_r($json);

$json= json_decode($json,true);

foreach($json as $value)
{
print “<pre>”;
print_r($value);
}

?>

Leave a comment

Constant keyword support only php5

<!– constant does not support php4 versiong
–>

<?php

//php 5 Support

class MyClass

{

const constant = ‘constant value’;
function showConstant()

{

echo  self::constant . “\n”;

}

}

$Obj = new MyClass();

$Obj->showConstant();
?>

 

 
<?php

//php 4 support

class MyClass

{

var $constant_1 = ‘constant value’;

function showConstant()

{

echo  $this->constant_1 . “\n”;

}

}

$Obj = new MyClass();

$Obj->showConstant();

 

?>

Leave a comment

static Keyword support php5

<!–
static Keyword –>

 

<?php
//php 5 Support

class visitors

{

private static $visitors = 0;

function __construct()

{            self::$visitors++;

}

static function getVisitors()

{            return self::$visitors;

}

 

}

 

//     Instantiate the visitors class.

$visits = new visitors();

echo visitors::getVisitors().”<br />”;

//Instantiate another visitors class.

$visits2 = new visitors();

echo visitors::getVisitors().”<br />”;

 

?>

Leave a comment

PHP 5 introduces abstract classes and methods.

<!–        PHP 5 introduces abstract classes and methods.        –>

<?php
//php 5 Support

abstract class employee

{       protected $empname;

protected $empage;

function setdata($empname,$empage)

{

$this->empname = $empname;

$this->empage = $empage;

}

 

abstract function outputData();

}

 

class EmployeeData extends employee   //extending abstract class

{

function __construct($name,$age)

{           $this->setdata($name,$age);

}

function outputData()

{          echo $this->empname;          echo $this->empage;

}

}

 

$a = new EmployeeData(“Hitesh”,”24″);

$a->outputData();

 

 

?>

Leave a comment

PH 5 introduces the final keyword

<!–
Final Keyword

PH 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final.

–>

<?php

//php 5 Support

class BaseClass

{   public function test()

{       echo “BaseClass::test() called\n”;

}

final public function moreTesting()

{       echo “BaseClass::moreTesting() called\n”;

}

}

 

class ChildClass extends BaseClass

{

public function moreTesting()

{

echo “ChildClass::moreTesting() called\n”;

}

}

// Results in Fatal error: Cannot override final method BaseClass::moreTesting()

?>

Leave a comment

Public, private , protected do not support php4 version

<!– public, private , protected do not support php4 version
–>

<?php //php 5 Support
class SimpleClass

{

 

public $var = ‘a default value’;
public function displayVar()

{        echo $this->var;

}

}
$Obj = new SimpleClass();$Obj->displayVar();

print “<br> Directly access (php5) ::”.$Obj->var;

?>

 

<?php

//php 4 support and php5 support

class SimpleClass

{

var $var = “a default value”;

function displayVar()

{

echo $this->var;

}

}
$Obj = new SimpleClass();$Obj->displayVar();

print “<br> Directly access  (php4) ::”.$Obj->var;

?>

Leave a comment

Opp 5 basic

<!-- opp5 ex 1 -->

<?php
class A{  function foo()  {  if(isset($this)) {  echo '$this is defined( ';  echo get_class($this);  echo ") <br>";   }  else  echo "$this is not defined <br>"; } }
class B{ public function bar() { A::foo();
} } $a = new A(); $a->foo(); A::foo(); $b = new B(); $b->bar(); // B::bar(); ?>
<!-- opp5 ex 2 -->
<?php
      class MyClass
	   {
	       public $public = 'This is public';
		   private $private = 'This is private';
		   protected $protected = 'This is protected';

            function printHello()
			 {
			    echo "<br>Call printHello MyClass $this->public <br>";
				echo "Call printHello MyClass  $this->private <br>";
				echo "Call printHello MyClass  $this->protected <br>";
			 }
	   }

	 $obj = new MyClass();

	 echo  $obj->public."<br>";
	// echo  $obj->private;
	// echo  $obj->protected;
	 $obj->printHello();

 class MyClass2 extends  MyClass
     {
	    protected $protected = 'protected 2';
		function printHello()
		 {
		   echo "Call printHello MyClass2 $this->private <br>";
		   echo "Call printHello MyClass2 $this->public <br>";
		   echo "Call printHello MyClass2 $this->protected <br>";
		 }
	 }	 

	 $obj2 = new MyClass2();
	 echo $obj2->public."<br>";   // Works
	 $obj2->PrintHello();  // Shows Public, Protected2, not Private
?>

<!-- opp5 ex 3 -->

<?php
   class Factorial
     {
	   private $number;
	   private $result=1;

	    function __construct($number)
		 {
		   $this->number = $number;
		 } 

		 function result()
		   {
		       for($i=2; $i<=$this->number; $i++)
			 {
			   $this->result*=$i;
			  }
			 return $this->result;
		    }
	  }
	  $a = new Factorial(5);
	  echo $a->result();
?>

  <?php
  class area
    {
	   public function calculate_area($length, $breadth)
	           {
			      return $length * $breadth;
	           }
    }

	  $result = area::calculate_area(225.3,56);
	  echo "The area is :".$result."  meters";
?>

<?php
class Connection {
    protected $link;
    private $server, $username, $password, $db;

    public function __construct($server, $username, $password, $db)
    {
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->connect();
    }

    private function connect()
    {
        $this->link = mysql_connect($this->server, $this->username, $this->password);
        mysql_select_db($this->db, $this->link);
    }

    public function __sleep()
    {
        return array('server', 'username', 'password', 'db');
    }

    public function __wakeup()
    {
        $this->connect();
    }

	public function show()
	{
	  $this->connect();
	   $results=mysql_query("select * from user");
	  while ($rows=mysql_fetch_array($results))
          {
          extract($rows);
           // echo $movie_name;
		echo "  $username   $password $fullname   $usertype   <br>";
		  // echo "$roll $username  $birth  $uploadfile  <br>";

          }
	}
}
   $obj = new Connection("localhost","root","abc12345","stuff");
   $obj->show();
?>

Leave a comment

Protected: Class Inheritance

This content is password protected. To view it please enter your password below:

Leave a comment

Protected: Instance ,attribute

This content is password protected. To view it please enter your password below:

Leave a comment