[PHP] Oefenen met classes

Gestart doorJosnl4 reacties39 weergaven

5 berichten · pagina 1 van 1

Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Hallo allen,

Ik werk al een aantal jaar met php, mysql, css, html en zo nu en dan ook javascript (jquery). Echter schrijf ik nu zulke grote webbased applicaties dat ik nu toch echt over wil stappen op OOP programmeren, dus (meer) met classes en functions gaan werken.

Nou heb ik voor mezelf een klein voorbeeldje geschreven, alleen krijg ik niet de juiste output, hij zegt alleen: array...

[COLOR= #0000BB]<?php[/COLOR]


[URL="http://www.php.net/class"]class[/URL] Eten [COLOR= #007700]{
    
    
    var[/COLOR] [COLOR= #0000BB]$voorgerecht[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Soep[/COLOR][COLOR= #007700];
    var[/COLOR] [COLOR= #0000BB]$hoofdgerecht[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Gegrilde kip[/COLOR][COLOR= #007700];
    var[/COLOR] [COLOR= #0000BB]$dessert[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Dame blanche[/COLOR][COLOR= #007700];[/COLOR]
    
    
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] Menu[COLOR= #007700]() {[/COLOR]
            
        [COLOR= #0000BB]$array_menu[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
        
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700])){[/COLOR]
            
            [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array_menu[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700]);            
            
        }[/COLOR]
        
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700])){[/COLOR]
            
            [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array_menu[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700]);            
            
        }[/COLOR]        
        
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$dessert[/COLOR][COLOR= #007700])){[/COLOR]
            
            [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array_menu[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dessert[/COLOR][COLOR= #007700]);            
            
        }[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$array_menu[/COLOR][COLOR= #007700];[/COLOR]
        
[COLOR= #FF8000]//AFSLUITEN FUNCTIE Menu();                [/COLOR]
            
    [COLOR= #007700]}[/COLOR]
    
[COLOR= #FF8000]//AFSLUITEN CLASS Eten();    [/COLOR]
    
[COLOR= #007700]}[/COLOR]


[COLOR= #0000BB]$object[/COLOR] [COLOR= #007700]= new[/COLOR] Eten[COLOR= #007700];[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$object[/COLOR][COLOR= #007700]->[/COLOR]Menu[COLOR= #007700]([/COLOR]array_menu[COLOR= #007700]);[/COLOR]


?>


Hij zou nu toch de array moeten echoen? Dit doet hij echter niet, krijg ook geen foutmelding.

Alvast bedankt voor jullie hulp! heart
Gr.
Josthumbsup
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Je echod een array. Een array is een object. Dus je krijgt de naam van het object terug: Array ()

Als je alle values van de array wilt hebben moet je print_r($object->Menu(array_menu)); doen

Overigens, wat is array_menu? Is dat een CONSTANT? Wat doe je ermee in je script? (de Menu() functie accepteerd geen parameters)

Bovendien moet je je variabelen een scope meegeven. Zijn de variabelen binnen de class private, public, protected?
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ik wilde om te proberen variabelen van buiten de functie, maar wel in een class, deze tonen in een array. Echter geeft ie nu niet Array, maar Array (), werkt dus helaas nog niet.

Wat bedoel je precies met een constant? Is het constant als het resultaat niet veranderd o.i.d.? De variabelen moeten volgens mij public zijn, moet ik dit er nog voor zetten?

En wat bedoel je precies met een scope?

Bedankt voor je hulp thumbsup
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Josnl schreef:
Ik wilde om te proberen variabelen van buiten de functie, maar wel in een class, deze tonen in een array. Echter geeft ie nu niet Array, maar Array (), werkt dus helaas nog niet.

Je moet werken met $this->variabelenaam in je functies
Dus i.p.v. $dessert doe je dan $this->desert
Josnl schreef:
Wat bedoel je precies met een constant? Is het constant als het resultaat niet veranderd o.i.d.? De variabelen moeten volgens mij public zijn, moet ik dit er nog voor zetten?

private. Omdat je variabelen van binnen je functie wilt aanroepen via een (public) functie. Dus je wil ze niet rechtstreeks aanroepen. Dus ze moeten private zijn. Enkel BINNEN de class (en parents / childs enz.) kun je ze aanroepen.
Josnl schreef:
En wat bedoel je precies met een scope?
Met de scope bedoelde ik meer die zooi van public, private en protected.
Josnl schreef:
Bedankt voor je hulp thumbsup
geen probleem.

Buiten het feit dat de class niet echt veel nut heeft zou ik het op zon soort manier aanpakken (je kan beter meteen een array gebruiken in plaats van te gaan pushen.):
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] Menu [COLOR= #007700]{[/COLOR]
    
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$dessert[/COLOR][COLOR= #007700];[/COLOR] 
    
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$array_menu[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]  
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$nagerecht[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700]) ||[/COLOR] [URL="http://www.php.net/empty"]empty[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]voorgerecht [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Soep[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]voorgerecht [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$voorgerecht[/COLOR][COLOR= #007700];
        }[/COLOR] 
           
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700]) ||[/COLOR] [URL="http://www.php.net/empty"]empty[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]hoofdgerecht [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Gegrilde kip[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]hoofdgerecht [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$hoofdgerecht[/COLOR][COLOR= #007700];
        }[/COLOR]   
         
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$nagerecht[/COLOR][COLOR= #007700]) ||[/COLOR] [URL="http://www.php.net/empty"]empty[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$nagerecht[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]nagerecht [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Dame blanche[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]nagerecht [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$nagerecht[/COLOR][COLOR= #007700];
        }  
    }[/COLOR]
    
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #007700]function[/COLOR] pushArray[COLOR= #007700]()
    {[/COLOR]   
        [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]array_menu[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]voorgerecht[COLOR= #007700]);[/COLOR]            
        [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]array_menu[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]hoofdgerecht[COLOR= #007700]);[/COLOR]            
        [URL="http://www.php.net/array_push"]array_push[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]array_menu[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]dessert[COLOR= #007700]);   
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getMenuArray[COLOR= #007700]() {[/COLOR]     
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]array_menu[COLOR= #007700]) ==[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]pushArray[COLOR= #007700]();
        }[/COLOR]
            
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$array_menu[/COLOR][COLOR= #007700];                       
    }  
    
}[/COLOR]

[COLOR= #0000BB]$object[/COLOR] [COLOR= #007700]= new[/COLOR] Menu[COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$object[/COLOR][COLOR= #007700]->[/COLOR]getMenuArray[COLOR= #007700]();[/COLOR]


?>
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Bedankt voor de duidelijke uitleg Rik, ook aan je voorbeeld heb ik wat, super! =D
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren