Controle van mijn class

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Senne Tijdeman

Senne Tijdeman

22/06/2009 21:23:00
Quote Anchor link
Hallo iedereen

Ik ben bezig om te oefenen met classes schrijven. Nou heb ik helaas mijn heilige php bijbel niet hier liggen, dus zouden jullie zo vriendelijk willen zijn om een blik te werpen op mijn code, en te voorzien van commentaar.

Dit is de class:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
class MakeForm {
    /*
     * Declare attributes
     */

    private $FormName;
    private $FormMethod;
    private $FormAction;
    private $FormClass;
    private $FormID;
    
    private $InputTitle;
    private $InputType;
    private $InputName;
    private $InputValue;
    private $InputClass;
    private $InputID;
    private $InputPossible;
    
    private $SelectTitle;
    private $SelectName;
    private $SelectValues;
    private $SelectClass;
    private $SelectID;
    
    private $ShowArray;
    
    private $Fieldset;
    
    public $Form;
    public $Input;
    public $Select;
    public $Title;
    public $Array;
    
    /*
     * * * Constructor * * *
     * Make the form
     */

    function __construct($Form_Name = NULL, $Form_Method = NULL, $Form_Action = NULL, $Form_Class = NULL, $Form_ID = NULL) {
        // Counter
        static $FormCounter = 1;
        
        // Check for Values
        if(!$Form_Name)   { $Form_Name   = 'MyForm_' . $FormCounter; }
        if(!$Form_Method) { $Form_Method = 'post'; }
        if(!$Form_Action) { $Form_Action = $_SERVER['PHP_SELF']; }
        if(!$Form_Class)  { $Form_Class  = false; }
        if(!$Form_ID)     { $Form_ID     = false; }
        
        // Give values
        $this->FormName   = $Form_Name;
        $this->FormMethod = $Form_Method;
        $this->FormAction = $Form_Action;
        $this->FormClass  = $Form_Class;
        $this->FormID     = $Form_ID;
        
        // Counter
        $FormCounter++;
        
        // Check if The id or class are full
        if($this->FormClass) { $this->FormClass = ' class="' . $this->FormClass . '"'; }
        if($this->FormID)    { $this->FormID    = ' id="' . $this->FormID . '"'; }
        
        // Begin the form
        $this->Form = '<form name="' . $this->FormName . '" method="' . $this->FormMethod . '" action="' . $this->FormAction . '"' . $this->FormClass . $this->FormID . '>' . "\n";
        
        return $this->Form;
    }

    /*
     * * * Desctuctor * * *
     * Stop the form
     */

    function __destruct() {
        // Close the Form
        $this->Form = '</form>' . "\n";
        
        echo $this->Form;
    }

    
    /*
     * * * input_field * * *
     * Make some inputs
     */

    function MakeInput($I_Title = NULL, $I_Type = NULL, $I_Name = NULL, $I_Value = NULL, $I_Class = NULL, $I_ID = NULL) {
        // Counter
        static $InputCounter = 1;
        
        // Check for Values
        if(!$I_Title) { $I_Title = 'MyInput_' . $InputCounter; }
        if(!$I_Type)  { $I_Type  = 'text'; }
        if(!$I_Name)  { $I_Name  = 'MyInput_' . $InputCounter; }
        if(!$I_Value) { $I_Value = false; }
        if(!$I_Class) { $I_Class = false; }
        if(!$I_ID)    { $I_ID    = false; }
        
        // Give values
        $this->InputTitle = $I_Title;
        $this->InputType  = $I_Type;
        $this->InputName  = $I_Name;
        $this->InputValue = $I_Value;
        $this->InputClass = $I_Class;
        $this->InputID    = $I_ID;
        
        // Make an array of possible type's
        $this->InputPossible = array(
            'text',
            'password',
            'radio',
            'checkbox',
            'hidden',
            'submit',
            'reset',
        );

        
        // Counter
        $InputCounter++;
        
        // Check if The value, id or class are full
        if($this->InputValue) { $this->InputValue = ' value="' . $this->InputValue . '"'; }
        if($this->InputClass) { $this->InputClass = ' class="' . $this->InputClass . '"'; }
        if($this->InputID)    { $this->InputID    = ' id="' . $this->InputID . '"'; }
        
        // Make stuff happen
        foreach ($this->InputPossible as $FormCheck) {
            if($this->InputType == $FormCheck) {
                $this->Title = '<label for="' . $this->InputName . '">' . $this->InputTitle . '</label>' . "\n";
                $this->Input = '<input title="' . $this->InputTitle . '" type="' . $this->InputType . '" name="' . $this->InputName . '"' . $this->InputValue . $this->InputClass . $this->InputID . ' />' . "\n";
            }
        }

        
        return $this->Title;
        return $this->Input;
    }

    
    /*
     * * * input_field * * *
     * Make some inputs
     */

    function MakeSelect($Select_Title = NULL, $Select_Name = NULL, $Select_Values = NULL, $Select_Class = NULL, $Select_ID = NULL) {
        // Counter
        static $SelectCounter = 1;
    
        // Check for Values
        if(!$Select_Title)  { $Select_Title = 'MySelect_' . $InputCounter; }
        if(!$Select_Name)   { $Select_Name  = 'MySelect_' . $InputCounter; }
        if(!$Select_Values) { $Select_Values = false; }
        if(!$Select_Class)  { $Select_Class = false; }
        if(!$Select_ID)     { $Select_ID    = false; }
        
        // Give values
        $this->SelectTitle  = $Select_Title;
        $this->SelectName   = $Select_Name;
        $this->SelectValues = $Select_Values;
        $this->SelectClass  = $Select_Class;
        $this->SelectID     = $Select_ID;
        
        // Counter
        $SelectCounter++;
        
        // Make select box
        $this->Title = '<label for="' . $this->SelectName . '">' . $this->SelectTitle . '</label>' . "\n";
        $this->Select  = '<select title="' . $this->InputTitle . '" name="' . $this->InputName . '">' . "\n";
        foreach ($this->SelectValues as $Value => $Option) {
            $this->Select .= '<option value="' . $Value . '">' . $Option . '</option>' . "\n";
        }

        $this->Select .= '</select>' . "\n";
        
        return $this->Title;
        return $this->Select;
    }

    
    /*
     * * * input_field * * *
     * Make some inputs
     */

    function ShowResult() {
        $value = false;
        $key   = false;
        
        foreach($_POST as $key => $value) {
            $this->Array .= '$' . $key . ' = $_POST["' . $key.'"];<br />'."\n";
        }

        
        return $this->Array;
    }
}

?>


En hier een voorbeeld van hoe ik het gebruik

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
echo '<div id="RegisterForm">'."\n";

$RegisterForm = new MakeForm('Register', 'post', '?page=register');
echo '    ' . $RegisterForm->Form;

echo '    <div class="RegisterField">'."\n";
$RegisterForm->MakeInput('E-mail', 'text', 'Email');
echo '        ' . $RegisterForm->Title;
echo '        ' . $RegisterForm->Input;
echo '    </div>'."\n";

echo '    <div class="RegisterField">'."\n";
$RegisterForm->MakeInput('Wachtwoord', 'password', 'Password1');
echo '        ' . $RegisterForm->Title;
echo '        ' . $RegisterForm->Input;
echo '    </div>'."\n";

echo '<div class="RegisterField">'."\n";
$RegisterForm->MakeInput('Nogmaals wachtwoord', 'password', 'Password2');
echo '        ' . $RegisterForm->Title;
echo '        ' . $RegisterForm->Input;
echo '</div>'."\n";

$SelectArray = array('1' => 'Gangster', '2' => 'Agent', '3' => 'Hoer');
echo '    <div class="RegisterField">'."\n";
$RegisterForm->MakeSelect('Wat wilt u worden', 'UserType', $SelectArray);
echo '        ' . $RegisterForm->Title;
echo '        ' . $RegisterForm->Select;
echo '    </div>'."\n";

echo '    <div class="RegisterButton">'."\n";
$RegisterForm->MakeInput(NULL, 'submit', 'submit', 'Versturen');
echo '        ' . $RegisterForm->Input;
echo '    </div>'."\n";

unset($RegisterForm);

echo '</div>'."\n";
?>
Gewijzigd op 01/01/1970 01:00:00 door Senne Tijdeman
 
PHP hulp

PHP hulp

27/04/2024 00:20:14
 
Joren de Wit

Joren de Wit

22/06/2009 23:19:00
Quote Anchor link
Je bent aardig op weg, maar er gaat wel iets wezenlijks mis. In je klasse heb je nu een aantal members staan die de eigenschappen van slechts 1 input element kunnen bevatten. Op dit moment misbruik je je object dus eigenlijk als HTML generator en dat is nu precies waar een object niet voor bedoeld is.

Als je het op een goede manier wilt aanpakken, zou je de elementen van je formulier ook als objecten moeten beschouwen. Met andere woorden, je hebt nog een klasse Input nodig, die je later bijvoorbeeld weer uit zou kunnen breiden tot SelectInput, TextInput, etc.

Je form klasse bevat vervolgens een verzameling van Input objecten die samen het formulier vormen. In een later stadium kun je dit weer uitbreiden met Validators die je aan de verschillende input objecten kunt koppelen.

Object geörienteerd denken

Lees bovenstaande tutorial eens door, dat geeft je een goed beeld van de denkwijze die je bij OOP moet hanteren.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.