stopwatch
Kris Peeters op 31/10/2013 09:54:09:
Ik snap eigenlijk niets van wat je in stop() probeert te doen.
Leg zelf eens uit wat je probeert te doen.
Kijk even
Leg zelf eens uit wat je probeert te doen.
Kijk even
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
public function stop()
{
$this->stopTime = microtime('now');
$timeview = $this->lap(); // lap() heeft geen return, dus $timeview zal gewoon null zijn.
$this->isRunning = false;
$timeview->startTime - $stopTime; //$timeview is null en heeft dus geen ->startTime. // En $stopTime bestaat niet
}
?
[/quote]
Ik probeer een class te maken voor een stopwatch. Nu wil ik als er op stop gedrukt is dat de tijd word uitgerekend. Dus je rekent uit de begintijd - de eindtijd en die geef je weer in seconden, uren en minuten
public function stop()
{
$this->stopTime = microtime('now');
$timeview = $this->lap(); // lap() heeft geen return, dus $timeview zal gewoon null zijn.
$this->isRunning = false;
$timeview->startTime - $stopTime; //$timeview is null en heeft dus geen ->startTime. // En $stopTime bestaat niet
}
?
[/quote]
Ik probeer een class te maken voor een stopwatch. Nu wil ik als er op stop gedrukt is dat de tijd word uitgerekend. Dus je rekent uit de begintijd - de eindtijd en die geef je weer in seconden, uren en minuten
Vooraleer je die kent, is het niet zinnig om te proberen iets echt te realiseren.
De betekenis van dit:
$timeview = $this->lap();
Dit betekent dat de functie lap() een return heeft. Die return wordt de nieuwe waarde van $timeview.
Maar, merk samen met ons op dat lap() geen return heeft.
Dus: waarom schrijf je dat? Dit lijkt ons onzinnig.
Dat soort dingen moet je echt goed snappen vooraleer je steeds grotere stukken code gaat schrijven.
@kris: ja snap ik, maar dit is nog allemaal nieuw voor mij.. Ik aardig op weg om dit te snappen. Ik vraag jullie om mij op de goede weg te helpen.
http://phptuts.nl/home/
En begin bij de beginners-tutorials.
"PHP Beginnershandleiding", daarna "OOP Beginnershandleiding (PHP5)".
Zorg dat je de principes goed door hebt.
Dit doe je door kleine oefeningen te maken; niet door direct aan een groot project te beginnen.
De principes moeten zo evident worden dat ze automatische reflexen worden.
Niets persoonlijk. Ik raad precies het zelfde aan, bij iedereen waarbij ik het gevoel heb dat ze te grote projecten willen realiseren vooraleer ze de onderdelen goed begrijpen.
Alleen nu wil ik controleren wat de timestate is. Zoals je onder in mijn script kan zien heb ik 2x het if statement geplaatst. Ik wil dus dat hij de timer opnieuw start ipv dat hij doorgaat.
Code (php)
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
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
<?php
interface IStopwatch
{
public function start();
public function stop();
public function lap();
}
class Stopwatch implements IStopwatch
{
private $startTime;
private $isRunning;
private $stopTime;
private $lapTime;
private $timeState;
function __construct()
{
$this->time = microtime(true);
}
public function start()
{
$this->startTime = microtime(true);
$this->isRunning = true;
return $this->startTime;
}
public function lap()
{
if(!$this->isRunning)
return false;
$this->lapTime = microtime(true);
$lapresult = $this->lapTime - $this->startTime;
return $lapresult;
}
public function stop()
{
$this->stopTime = microtime(true);
$endresult = $this->stopTime - $this->startTime;
return $endresult;
}
}
$stopwatch = new Stopwatch();
if($stopwatch->start())
{
echo 'stopwatch loopt nu';
usleep(1320000);
}
echo PHP_EOL;
if($laptime = $stopwatch->lap())
{
echo 'laptime:' . round($laptime,3);
}
usleep(1200000);
echo PHP_EOL;
if($stoptime = $stopwatch->stop())
{
echo 'stoptime: ' . round($stoptime,3);
echo PHP_EOL;
echo 'de stopwatch is nu gestopt';
}
else
{
echo 'stopwatch loopt niet';
}
echo PHP_EOL;
if($stopwatch->stop())
{
echo 'stopwatch loopt nu';
usleep(1320000);
}
echo PHP_EOL;
if($laptime = $stopwatch->lap())
{
echo 'laptime:' . round($laptime,3);
}
usleep(1200000);
echo PHP_EOL;
if($stoptime = $stopwatch->stop())
{
echo 'stoptime: ' . round($stoptime,3);
echo PHP_EOL;
echo 'de stopwatch is nu gestopt';
}
else
{
echo 'stopwatch loopt niet';
}
interface IStopwatch
{
public function start();
public function stop();
public function lap();
}
class Stopwatch implements IStopwatch
{
private $startTime;
private $isRunning;
private $stopTime;
private $lapTime;
private $timeState;
function __construct()
{
$this->time = microtime(true);
}
public function start()
{
$this->startTime = microtime(true);
$this->isRunning = true;
return $this->startTime;
}
public function lap()
{
if(!$this->isRunning)
return false;
$this->lapTime = microtime(true);
$lapresult = $this->lapTime - $this->startTime;
return $lapresult;
}
public function stop()
{
$this->stopTime = microtime(true);
$endresult = $this->stopTime - $this->startTime;
return $endresult;
}
}
$stopwatch = new Stopwatch();
if($stopwatch->start())
{
echo 'stopwatch loopt nu';
usleep(1320000);
}
echo PHP_EOL;
if($laptime = $stopwatch->lap())
{
echo 'laptime:' . round($laptime,3);
}
usleep(1200000);
echo PHP_EOL;
if($stoptime = $stopwatch->stop())
{
echo 'stoptime: ' . round($stoptime,3);
echo PHP_EOL;
echo 'de stopwatch is nu gestopt';
}
else
{
echo 'stopwatch loopt niet';
}
echo PHP_EOL;
if($stopwatch->stop())
{
echo 'stopwatch loopt nu';
usleep(1320000);
}
echo PHP_EOL;
if($laptime = $stopwatch->lap())
{
echo 'laptime:' . round($laptime,3);
}
usleep(1200000);
echo PHP_EOL;
if($stoptime = $stopwatch->stop())
{
echo 'stoptime: ' . round($stoptime,3);
echo PHP_EOL;
echo 'de stopwatch is nu gestopt';
}
else
{
echo 'stopwatch loopt niet';
}
Gewijzigd op 06/11/2013 16:56:30 door Dirk Renes