[php] [class] String gebruiken

Gestart doorFreestyle19 reacties8 weergaven

20 berichten · pagina 1 van 2

Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Als ik een class hebt met 2 functies. Hoe kan ik dan de string in de bovenste class in de 2de functie gebruiken??

Ik heb nu zoietS:

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700];[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] a[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]test[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] b[COLOR= #007700]() {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test[COLOR= #007700];    
    }
}[/COLOR]

[COLOR= #0000BB]$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>


Maar dit wilt niet werken, dus hoe krijg ik dit dan werkend?

Alvast bedankt.
0
Berichten
6.722
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Je moet eerst a() oproepen voordat je b() doet. En een class oproepen moet ook met haakjes erachter:

[COLOR= #0000BB]<?php
$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]a[COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>
0
Berichten
5.897
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Of:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700];[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] a[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]test[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] b[COLOR= #007700]() {[/COLOR]
        self[COLOR= #007700]::[/COLOR]a[COLOR= #007700]();[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test[COLOR= #007700];    
    }
}[/COLOR]

[COLOR= #0000BB]$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Maartenl schreef:
Of:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700];[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] a[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]test[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] b[COLOR= #007700]() {[/COLOR]
        self[COLOR= #007700]::[/COLOR]a[COLOR= #007700]();[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test[COLOR= #007700];    
    }
}[/COLOR]

[COLOR= #0000BB]$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>
self:: is vooral voor statische functies. In dit geval is het geen statische functie en moet je dus $this-> ipv self:: gebruiken.
0
Berichten
5.897
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Eerdere citaten tonen
Maartenl schreef:
Of:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700];[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] a[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]test[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] b[COLOR= #007700]() {[/COLOR]
        self[COLOR= #007700]::[/COLOR]a[COLOR= #007700]();[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test[COLOR= #007700];    
    }
}[/COLOR]

[COLOR= #0000BB]$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>

self:: is voor statische functies. In dit geval is het geen statische functie en moet je dus $this-> ipv self:: gebruiken.

Och redfaceHier werkte het gewoon yummie
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ja, het werkt ook wel, maar over het algemeen is dat wat een beginnende programmeur moet gebruiken.

Je hebt $this-> dat verwijst naar het huidige object, waar self:: verwijst naar de huidige class als ik me niet vergis. Zo heb je ook nog dingen als parent:: static:: en nog een paar. Maar dat is allemaal te moeilijk.
0
Berichten
3.314
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dat heeft te maken met class inheritance, bijv als je class animal met method eat hebt kun je en object dog met als class animal maken, met een eigen method eat.

Stel dat de parent, animal, in de eat method zegt "Dit dier eet"

en dat de hond in de eat method zegt hondenbrokken

Dan kun je de parent van de class, de animal, dus aanroepen voordat je de eat method van de hond zelf aanroept.

class Dog extends Animal{
public eatDisplay(){
echo parent::eat();
echo self::eat();

}

Geeft dan dit dier eet hondenbrokken
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
^Zoals ik dus al zei: Voor dit voorbeeld te moeilijk.
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Maar hoe moet ik het dan nu bij mij gebruiken?
self:: niet toch??
Maar zo doet hij het niet.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700];[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] a[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]test[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] b[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]a[COLOR= #007700]();[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]test[COLOR= #007700];    
    }
}[/COLOR]

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


Dit zou gewoon moeten werken.
0
Berichten
3.314
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
^Zoals ik dus al zei: Voor dit voorbeeld te moeilijk.
Ik weet dat je t weet en je hebt gelijk, was alleen ff als uitleg voor TS yummie
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Bedankt! het werkt nu smile
0
Berichten
6.144
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Trouwens nooit een echo gebruiken in een class/functie.
Altijd return en bij het oproepen van iets pas echo.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Jponsen schreef:
Trouwens nooit een echo gebruiken in een class/functie.
Altijd return en bij het oproepen van iets pas echo.
Niet altijd. Maar over het algemeen wel ja. Dingen als Exceptions moet je vrijwel zeker binnen de class zelf echoen. En zo zijn er nog wel wat voorbeelden waarbij dat moet.
Maar voor de rest geef ik je wel gelijk. In 99% van de gevallen moet je waardes returnen.
0
Berichten
4.912
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Het volgende kan wel

[COLOR= #0000BB]<?php[/COLOR]
    [URL="http://www.php.net/class"]class[/URL] test [COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$_test[/COLOR][COLOR= #007700];
        
        function[/COLOR] __construct[COLOR= #007700]() {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]_test [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]hoi[/COLOR][COLOR= #007700];
        }
        function[/COLOR] b[COLOR= #007700]() {[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]_test[COLOR= #007700];
        }
    }[/COLOR]
    [COLOR= #0000BB]$test[/COLOR] [COLOR= #007700]= new[/COLOR] test[COLOR= #007700]();[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$test[/COLOR][COLOR= #007700]->[/COLOR]b[COLOR= #007700]();[/COLOR]
?>
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