Dynamic form class for getting value from a form - php

I'm using a Form class for getting the values of a form in static way. Everything is great. But I want to do it dynamically. I want a form class that will do the job for different forms. In display() method I'm getting value of name, email, password, phone etc. I want that when there are more or less value or in another form the Form class do the job for me dynamically. How can i do that?
//This is Register.php
public function display()
{
Form::setname($_POST['name']);
Form::email($_POST['email']);
Form::password($_POST['pass']);
Form::repassword($_POST['rpass']);
Form::phone($_POST['phone']);
list($name,$b,$c,$d,$e)=Form::getall();
}
<?php
//This is Form.php
class Form
{
private $name;
private $email;
private $pass;
private $rpass;
private $phone;
public static function setname($name)
{
$this->name=$name; // Using $this when not in object context
}
public static function email($email)
{
$this->email=$email;
}
public static function password($pass)
{
$this->pass=$pass;
}
public static function repassword($rpass)
{
$this->rpass=$rpass;
}
public static function phone($phone)
{
$this->phone=$phone;
}
public static function getall()
{
$a=$this->name;
$b=$this->email;
$c=$this->pass;
$d=$this->rpass;
$e=$this->phone;
return [$a,$b,$c,$d,$e];
}
}

There are a few things you have to do to get this right. First, avoid statics. Conceptually each form should be represented by it's own object. Second, use magic methods provided by PHP. Those are very powerful and if used properly can allow for some crazy good designs. Third, use array notation with a single name for all your input elements within a single form, for example, for the name of your form elements: use something like: User[email] instead of just email and User[name] instead of just name and so on and so forth.
Keeping these in mind, the form class could be as follows:
class Form{
private $variables = array();
public function __get($name){
$returnValue = null;
if(isset($this->variables[$name])){
$returnValue = $this->variables[$name];
}
return $returnValue;
}
public function __set($name, $value){
$this->variables[$name] = $value;
}
public function getAll(){
return $this->variables;
}
}
This should be enough for the functionality that you require. In addition, you can add a convenience function that I have found very useful. This could be named as a setAttrubites or setAll function. It would be like this:
public function setAll($allData){
foreach($allData as $key => $data){
$this->variables[$key] = $data;
}
}
This will allow you to set all variables in one swoop using a command like this:
$form = new Form();
$form->setAll($_POST['User']);
In order to make this possible, as I mentioned before, all your input elements should be grouped in an array. So the input elements would be like this:
<input type="text" name="User[name]" />
<input type="text" name="User[email]" />
Hope you get the drift...

You can try to add an array of new variables to the class, much like
$dynamic_form_elements = array();
public static function dynamic_element($element, $value) {
$this->dynamic_form_elements[$element] = $value;
}
After that, simply put the $dynamic_form_elements variable to your list for returning in the getAll() method.
return [$a,$b,$c,$d,$e,$dynamic_form_elements];

It's working fine I just needed a setvalue and getvalue method in Form calss .And call it each time for a specific field in display() method.Is it good or there are any better way?
class Form{
private $value;
public function setvalue($value)
{
$this->value=$value;
}
public function getvalue()
{
$a=$this->value;
return $a;
}
}
public function display()
{
$newform=new Form();
$newform->setvalue($_POST['name']);
$name=$newform->getvalue();
$newform->setvalue($_POST['email']);
$b=$newform->getvalue();
$newform->setvalue($_POST['pass']);
$c=$newform->getvalue();
$newform->setvalue($_POST['rpass']);
$d=$newform->getvalue();
$newform->setvalue($_POST['phone']);
$e=$newform->getvalue();
}

Related

dynamically created "protected static" variables in a php class

class KD_DB extends PDO {
protected static $dbOne = '';
protected static $dbTwo = '';
public function setVariable ($alias,$content){
switch($alias){
case'one': self::$dbOne = $content; break;
case'two': self::$dbTwo = $content; break;
}
}
}
Is there a way to create these dynamically?
Something like this to create the protected static variables
public function setVariable ($alias,$content){
self::${$alias} = $content;
}
It did not work, but I suspect it is because I need it to be static to make it to work with a third class that extends this one...
If you only have the two variables, it may be easier (with more appropriate names) to set them using a static function for each one, something like...
class KD_DB {
public static $dbOne = '';
public static $dbTwo = '';
public static function setOne ($content){
self::$dbOne = $content;
}
}
KD_DB::setOne("value for one");
var_dump(KD_DB::$dbOne);
(code with minor changes to show the process)
But if you wanted a more open ended method, I would go for an associative array as the static variables and then use the 1 method (like you currently are) to set the value in the array...
class KD_DB {
public static $data = [];
public static function setVariable ($alias,$content){
self::$data[$alias] = $content;
}
}
KD_DB::setVariable("three", "value for three");
var_dump(KD_DB::$data);
this method can have issues if you mistype a variable reference which should be found during testing though, but does offer flexibility.

PHP Create Local Variables From String

Is there a way to create a variable inside an object from a string? for example:
class Whatever {
public function createVariables() {
$this["variable_name"] = 100;
}
}
I want to dynamically create variables depending on a set of rules that are defined by a child class.
class Whatever {
public function createAttributes($attribute_name, $attribute_value = NULL) {
$this->{$attribute_name} = $attribute_value;
}
}
This is the way in PHP to add attributes dynamically, nothing related specially to laravel
Laravel also has a built-in way https://github.com/illuminate/database/blob/v4.2.17/Eloquent/Model.php#L2551
class Whatever {
protected $id;
public function createAttributes($id) {
$this->id = $id;
return $this;
}
}
You can call this variable as $this->id inside another functions

Attempt to assign property of non-object error

I am getting this error and i can't see what i am doing wrong. I have done the same thing with other objects from other classes which are built in the exact same way and i can't see why i am getting this error now.
The code in which i create the object is this one:
$consulta2 = "SELECT * FROM TiposDireccion WHERE Cliente_CIF='$cif' and Direccion_Direccion='$direccion' and Direccion_CP=$cp ";
echo($consulta2."</br>");
if ($resultado2 = $conexion->query($consulta2)){
while($fila2 = $resultado2->fetch_object()){
$tipodireccion78=$fila2->TipoDireccion_Tipo;
//we see here that the select is returning a correct string with a correct value
echo($tipodireccion78);
//we try to instantiate and it fails =(
$unTipoDireccion=TipoDireccion::constructor1($tipodireccion78);
This is the class TipoDireccion:
<?php
class TipoDireccion{
private $tipo;
private $descripcion;
//Construct auxiliar
function __construct() {
}
//Constructor 1 : completo
function constructor1($tipo) {
$tipoDireccion = new TipoDireccion();
$tipoDireccion->tipo = $tipo;
return $tipoDireccion;
}
function ponTipo($tipo) {
$this->tipo = $tipo;
}
function devuelveTipo() {
return $this->tipo;
}
function ponDescripcion($descripcion) {
$this->descripcion = $descripcion;
}
function devuelveDescripcion() {
return $this->descripcion;
}
}
?>
Thank you a lot in advance!
Don't know if this is still relevant to you, but in case anyone else comes on here for an answer. The problem is in this function:
function constructor1($tipo) {
$tipoDireccion = new TipoDireccion();
$tipoDireccion->tipo = $tipo;
return $tipoDireccion;
}
Because in the class definition, you define private $tipo; and then you try and assign $tipoDireccion->tipo to what was passed through the function. However, you aren't trying to access that variable through the scope of the class, you are trying to assign it from the 'public' scope as far as the class is concerned.
The fix for this has two options, the first one would be to change private $tipo; to public $tipo;. But that isn't a good solution as you have an assignment function for it.
Instead, use your functions that you made, which would make the function look like:
function constructor1($tipo) {
$tipoDireccion = new TipoDireccion();
$tipoDireccion->ponTipo($tipo);
return $tipoDireccion;
}
That's how you need to access it from the public scope, which you are doing after you initiate a new one.
function constructor1($tipo) {}
should be
static function constructor1($tipo) {}

PHP - passing variable from one object to another troubles

I've recently started to work with OO PHP. As a training practice I'm trying to write some simple classes. I have trouble passing a variable from one to another class. Is it even possible?
class group
{
public $array = array();
public function person($name,$surname)
{
$this->person = new person($name,$surname);
}
public function __destruct()
{
print_r($this->array);
}
}
class person
{
public function __construct($name,$surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
$A = new group();
$A->person("John","Doe");
What I want to archieve here is to pass person as another member of group (by simply putting it in group array) for further modifications and sorting. Been googling around but found nothing.
Please forgive me if it's a dumb one. ;)
I'm not sure I totally understand but I think you want:
Class group {
public $members=array();
public function person($name,$surname) {
$this->members[]=new person($name,$surname);
//Creates a new person object and adds it to the internal array.
}
/*...*/
}
A better alternative (seperation of intent) would be:
Class group {
public $members=array();
public function addPerson(person $p) {
$this->members[]=$p;
//Avoids this function need to know how to construct a person object
// which means you can change the constructor, or add other properties
// to the person object before passing it to this group.
}
/*...*/
}
The fix is changing
public function person($name,$surname)
{
$this->person = new person($name,$surname);
}
to
public function person($name,$surname)
{
$this->array[] = new person($name,$surname);
}
$this->person is not being stored in the array otherwise, and is overwritten with each call.
Your group class could improve it's OO by:
changing $array to be more descriptively named
changing the function name person to something more meaningful, like add_person
You should define your properties ('name', 'surname') and give them a suitability visibility
class group
{
public $array = array();
public name;
public surname;
...
Reference: http://php.net/manual/en/language.oop5.visibility.php

Templating system scoping issue

I'm trying to whip up a skeleton View system in PHP, but I can't figure out how to get embedded views to receive their parent's variables. For example:
View Class
class View
{
private $_vars=array();
private $_file;
public function __construct($file)
{
$this->_file='views/'.$file.'.php';
}
public function set($var, $value=null)
{
if (is_array($var))
{
$this->_vars=array_merge($var, $this->_vars);
}
else
$this->_vars[$var]=$value;
return $this;
}
public function output()
{
if (count($this->_vars))
extract($this->_vars, EXTR_REFS);
require($this->_file);
exit;
}
public static function factory($file)
{
return new self($file);
}
}
test.php (top level view)
<html>
<body>
Hey <?=$name?>! This is <?=$adj?>!
<?=View::factory('embed')->output()?>
</body>
</html>
embed.php (embedded in test.php
<html>
<body>
Hey <?=$name?>! This is an embedded view file!!
</body>
</html>
Code:
$vars=array(
'name' => 'ryan',
'adj' => 'cool'
);
View::factory('test')->set($vars)->output();
Output:
Hey ryan! This is cool! Hey [error for $name not being defined]
this is an embedded view file!!
The problem is the variables I set in the top level view do not get passed to the embedded view. How could I make that happen?
So, I'm not exactly answering your question, but here's my super-simple hand-grown template system. It supports what you're trying to do, although the interface is different.
// Usage
$main = new SimpleTemplate("templating/html.php");
$main->extract($someObject);
$main->extract($someArray);
$main->name = "my name";
$subTemplate = new SimpleTemplate("templating/another.php");
$subTemplate->parent($main);
$main->placeholderForAnotherTemplate = $subTemplate->run();
echo $main; // or $main->run();
// html.php
<html><body><h1>Title <?= $name ?></h1><p><?= $placeHolderForAnotherTemplate ?></p></body></html>
<?php
// SimpleTemplate.php
function object_to_array($object)
{
$array = array();
foreach($object as $property => $value)
{
$array[$property] = $value;
}
return $array;
}
class SimpleTemplate
{
public $source;
public $path;
public $result;
public $parent;
public function SimpleTemplate($path=false, $source=false)
{
$this->source = array();
$this->extract($source);
$this->path($path);
}
public function __toString()
{
return $this->run();
}
public function extract($source)
{
if ($source)
{
foreach ($source as $property => $value)
{
$this->source[$property] = $value;
}
}
}
public function parent($parent)
{
$this->parent = $parent;
}
public function path($path)
{
$this->path = $path;
}
public function __set($name, $value)
{
$this->source[$name] = $value;
}
public function __get($name)
{
return isset($this->source[$name]) ? $this->source[$name] : "";
}
public function mergeSource()
{
if (isset($this->parent))
return array_merge($this->parent->mergeSource(), $this->source);
else
return $this->source;
}
public function run()
{
ob_start();
extract ($this->mergeSource());
include $this->path;
$this->result = ob_get_contents();
ob_end_clean();
return $this->result;
}
}
well, you create a new instance of the class, so there are no variables defined in the embedded template. you should try to copy the object, rather than creating a new one.
edit: I'm talking about the factory method
The main issue is that your views have no direct knowledge of each other. By calling this:
<?=View::factory('embed')->output()?>
in your "parent" view, you create and output a template that has no knowledge of the fact that it is inside another template.
There are two approaches I could recommend here.
#1 - Associate your templates.
By making your embedded templates "children" of a parent template, you could allow them to have access to the parent's variables at output() time. I utilize this approach in a View system I built. It goes something like this:
$pView = new View_Parent_Class();
$cView = new View_Child_Class();
$pView->addView($cView);
At $pview->render() time, the child view is easily given access to the parent's variables.
This method might require a lot of refactoring for you, so I'll leave out the dirty details, and go into the second approach.
#2 - Pass the parent variables
This would probably be the easiest method to implement given the approach you've taken so far. Add an optional parameter to your output method, and rewrite it slightly, like this:
public function output($extra_vars = null)
{
if (count($this->_vars))
extract($this->_vars, EXTR_REFS);
if (is_array($extra_vars)) extract($extra_vars, EXTR_REFS);
require($this->_file);
exit;
}
If you add a simple getter method as well:
public function get_vars()
{
return $this->_vars;
}
Then you can embed your files with what is effectively read-access to the parent's variables:
<?=View::factory('embed')->output($this->get_vars())?>
$this will be a reference to the current template, ie. the parent. Note that you can have variable name collisions via this method because of the two extract calls.
You could make your $_vars property static, not particularly elegant, but would work for what you are trying to achieve.
On a side note... your array_merge() in the set() function is wrong, swap your 2 variables around.

Categories