Posts Tagged ‘php oop’

properties & pass variable form to php

March 22nd, 2016

access private properties and pass
variable form to php part 2

The part one shown you guys about how to use extract method for converted

all post which submit by HTML form to array.

In this part I would present that bring variable from HTML

from with method=”POST” to list in

array and access private properties via method.

and you can see the new style of generated $_REQUEST:

function setvalue($key) {
if(isset($_REQUEST[$key])) echo $_REQUEST[$key];
}

if($_SERVER[‘REQUEST_METHOD’] == “POST”){
extract($_POST);

You can check below code for detail:

Cal1.php


<?php
class Cal1{

private $x;
private $y;

function __construct($a = NULL ,$b = NULL){
$this->x = $a;
$this->y = $b;
}

function sum() {
$sum = $this->x + $this->y;
return $sum;
}

function sub() {
return $this->x - $this->y;
}

function div() {
return $this->x / $this->y;
}

function mul() {
return $this->x * $this->y;
}

}
?>

Viewcal5.php

<?php
include ('libs/Cal1.php');
function setvalue($key) {
if(isset($_REQUEST[$key])) echo $_REQUEST[$key];
}
$result ="";
if($_SERVER['REQUEST_METHOD'] == "POST"){
extract($_POST);
$obj = new Cal1($a,$b);
$opt = end($_POST);
$o = array_search($opt,$_POST);
$result = $obj->{$o}();
}
?>
<!DOCTYPE html>
<head>
<title>Access to private properties</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
A: <input type="text" name="a" value="<?php setvalue("a");?>"><br />
B: <input type="text" name="b" value="<?php setvalue("b"); ?>"><br />
Result: <input type="text" value="<?php echo $result;?>" />
<input type="submit" name="sum" value="+" />
<input type="submit" name="sub" value="-" />
<input type="submit" name="mul" value="*" />
<input type="submit" name="div" value="/" />
</form>
</body>

access private properties and pass variable form to php part 1

March 17th, 2016

access private properties and pass variable form to php part 1

Below code is how to access private class by its method and pass through variable in HTML form to PHP code.

First we need coding class and assign the name of the file called Cal1.php.

Second we create HTML from with some variables and try to pass its to php code using extract() method and assign the file called viewcal1.php.

  • extract() method is the method that abstract the global variable $_POST after submit to array key in php.

Finally, you can test your code

Cal1.php

Cal1.php

 

 

 

 

 

 

 

 

 

 

viewcal1

viewcal1

php class and it properties

March 4th, 2016

In this step I would like to show some identify php class and it properties also identify object of the class as below:

  • class person is identify the class name
  • var $name; var $age; var $gender is proerties of the class person
  • $obj = new Person(); identify variable $obj as object of class Person
  • so $obj can access all properties and method in class person

<?php
class Person {
var $name;
var $age;
var $gender;
}
$obj = new Person();

$obj->name ="Dara";
$obj->age = 15;
$obj->gender = "Male";

echo "Person Name: $obj->name <br />";
echo "Person Age: $obj->age <br />";
echo "Person Gender: $obj->gender <br />";
?>

Facebook Auto Publish Powered By : XYZScripts.com