summaryrefslogtreecommitdiff
path: root/devel/sample.cpp.php
blob: b85229d91e818747464d5b67200cc6537d03c4f9 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
<?php
#if PHP_VERSION >= 500
#	define PublicMethod public
#else
#	define ClassName classname
#	define PublicMethod
#	define abstract
#	define innerIf_ innerif_
#	define emptySwitch emptyswitch
#	define defaultSwitch defaultswitch
#endif
#if PHP_VERSION >= 520
#else
#	define __callStatic __callstatic
#	define __toString __tostring
#endif
#if PHP_VERSION >= 530

namespace ns;
#endif

abstract class ClassName
{
#if PHP_VERSION >= 500
	const File = __FILE__;
	const ClassName = __CLASS__;
#if PHP_VERSION >= 530
	const Dir = __DIR__;
#endif
	const CONST_VALUE = 'A constant value';

	/** doc */
	static public $static = array(
		0           => array('array'),
		1           => 'str',
		CONST_VALUE => CONST_VALUE
		);
	static public $classProp;
	static public $static_const1 = CONST_VALUE;
	static public $static_const2 = self::CONST_VALUE;
	static public $static_const3 = ClassName::CONST_VALUE;
	static public $static_const4 = array(CONST_VALUE => 'test');
	static public $static_const5 = array(self::CONST_VALUE => 'test');
	static public $static_const6 = array(ClassName::CONST_VALUE => 'test');
	static public $static_const7 = array('test' => CONST_VALUE);
	static public $static_const8 = array('test' => self::CONST_VALUE);
	static public $static_const9 = array('test' => ClassName::CONST_VALUE);
	static public $static_const10 = array(CONST_VALUE => CONST_VALUE);
	static public $static_const11 = array(self::CONST_VALUE => self::CONST_VALUE);
	static public $static_const12 = array(ClassName::CONST_VALUE => ClassName::CONST_VALUE);
#if PHP_VERSION >= 560
	static public $ast_binop = ClassName::CONST_VALUE + ClassName::CONST_VALUE;
	static public $ast_and = ClassName::CONST_VALUE && 1;
	static public $ast_or = ClassName::CONST_VALUE || 2;
	static public $ast_select = ClassName::CONST_VALUE ? a : b;
	static public $ast_unaryPlus = +ClassName::CONST_VALUE;
	static public $ast_unaryMinus = -ClassName::CONST_VALUE;
#endif
	/** doc */
	static public $public_static = array(2, 'str');
	/** doc */
	static private $private_static = array(2, 'str');
	static private $private_static2 = array(self::CONST_VALUE => self::CONST_VALUE);
	/** doc */
	static protected $protected_static = array(2, 'str');
	static protected $protected_static2 = array(self::CONST_VALUE => self::CONST_VALUE);
	/** doc */
	public $property = array(
		array('array'),
		'str'
		);
	/** doc */
	public $public_property = array(2, 'str');
	/** doc */
	private $private_property = array(2, 'str');
	/** doc */
	protected $protected_property = array(2, 'str');
	public $array = array();
#else
	var $property = array(
		array('array'),
		'str'
		);
#endif

#if PHP_VERSION >= 500
	/** doc */
#endif
	PublicMethod function f1()
	{
	}

	PublicMethod function f2()
	{
	}

	PublicMethod function __construct($arg1, $arg2)
	{
		if (0) {
			ClassName::__construct();
		}
	}

	PublicMethod function codeStatic()
	{
		static $array = array(
			0           => array('array'),
			1           => 'str',
			CONST_VALUE => CONST_VALUE
			);
		static $static = 1;
		static $str = 'string';
		echo CONST_VALUE;
#if PHP_VERSION >= 500
		echo ClassName::CONST_VALUE;
		empty(ClassName::$classProp);
		isset(ClassName::$classProp);
		ClassName::$classProp = 1;
		echo ClassName::$classProp;
#endif
#if PHP_VERSION >= 530
		echo $this::CONST_VALUE;
		echo $object::CONST_VALUE;
		echo CONST_VALUE;
		$this::method();
		$object::method();
#endif
	}

	PublicMethod function codePropertyIndex()
	{
		$object = $this;
		$object->a = 1;
		$object->b = 2;
		$object->prop = 'prop';
		empty($object->objProp);
		isset($object->objProp);
#if PHP_VERSION >= 500
		unset($object->objProp);
#endif
		$object->objProp = 1;
		echo $object->objProp;
		empty($this->thisProp);
		isset($this->thisProp);
#if PHP_VERSION >= 500
		unset($this->thisProp);
#endif
		$this->thisProp = 1;
		echo $this->thisProp;
#if PHP_VERSION >= 500
		unset($array['index']->valueProp);
#endif
		unset($object->array['index']);
		unset($this->array['index']);
		empty($_GET['get']);
		isset($_GET['get']);
		unset($_GET['get']);
		$_GET['get'] = 1;
		echo $_GET['get'];
		isset($GLOBALS['global']);
		empty($GLOBALS['global']);
		unset($GLOBALS['global']);
		$GLOBALS['global'] = 1;
		echo $GLOBALS['global'];
		empty($array['index']);
		isset($array['index']);
		unset($array['index']);
		$array['index'] = 1;
		echo $array['index'];
		empty($array['index']->indexProp);
		isset($array['index']->indexProp);
#if PHP_VERSION >= 500
		unset($array['index']->indexProp);
#endif
		$array['index'] = $object;
		$array['index'] .= $object;
		$array['index']->indexProp = 1;
		echo $array['index']->indexProp;
		$GLOBALS['var'] = $object;
		empty($GLOBALS['var']->indexProp);
		isset($GLOBALS['var']->indexProp);
#if PHP_VERSION >= 500
		unset($GLOBALS['var']->indexProp);
#endif
		$GLOBALS['var']->indexProp = 1;
		echo $GLOBALS['var']->indexProp;
	}

	PublicMethod function code1()
	{
		$method = 'method';
		ClassName::$method();
		$this->method();
	}

	PublicMethod function codeConstant()
	{
		echo __CLASS__;
		echo __METHOD__;
		echo __FUNCTION__;
	}

	PublicMethod function codeExpression()
	{
		$a = 1;
		$b = $c = 2;
		$a = $b + $c;
		$a = $b + 1;
		$a = 1 + $b;
		$a = $b - $c;
		$a = $b * $c;
		$a = $b / $c;
		$a = $b % $c;
		$a = $b . $c;
		$a = $b = $c;
		$a = $b & $c;
		$a = $b | $c;
		$a = $b ^ $c;
		$a = ~$b;
		$a = -$b;
		$a = +$b;
		$a = $b >> $c;
		$a = $b >> $c;
		$a = $b == $c;
		$a = $b === $c;
		$a = $b != $c;
		$a = $b < $c;
		$a = $b <= $c;
		$a = $b <= $c;
		$a = $b++;
		$a = ++$b;
		$a = $object->b++;
		$a = ++$object->b;
		$a = $b--;
		$a = --$b;
		$a = $object->b--;
		$a = --$object->b;
		$a = !$b;
		$a = $b === $c;
		$a = $b !== $c;
		$a = $b << 2;
		$a = $b >> 3;
		$a += $b;
		$a -= $b;
		$a *= $b;
		$a /= $b;
		$a <<= $b;
		$a >>= $b;
		$a &= $b;
		$a |= $b;
		$a .= $b;
		$a %= $b;
		$a ^= $b;
		$a = 'a' . 'b';
		$a = 'a' . 'abc';
		$a = "a$abc d"; // RESULT: 'a' . $abc . ' d';
		$a = __FILE__;
#if PHP_VERSION >= 530
		$a = __DIR__;
#endif
		$a = 'a' . __FILE__;
#if PHP_VERSION >= 530
		$a = 'a' . __DIR__;
#endif
		@f1();
		print('1');
		$array = array('index' => 1);
		$a = $array['index'];
		$a = $object->prop;
		$a = $this->prop;
		$array['index'] = 1;
		$object->prop = 1;
		$this->prop = 1;
		$a = isset($b);
		$a = empty($b);
		unset($b);
		$b = 1;
		$a = isset($array['index']);
		$a = empty($array['index']);
		unset($array['index']);
		$a = isset($object->prop);
		$a = empty($object->prop);
#if PHP_VERSION >= 500
		unset($object->prop);
#endif
		$a = isset($this->prop);
		$a = empty($this->prop);
#if PHP_VERSION >= 500
		unset($this->prop);
		$a = isset(ClassName::$prop);
		$a = empty(ClassName::$prop);
#endif
	}

	PublicMethod function codeCast()
	{
		$a = (int) $b;
		$a = (double) $b;
		$a = (string) $b;
		$a = (array) $b;
		$a = (object) $b;
		$a = (bool) $b;
		$a = (unset) $b;
		$a = (array) $b;
		$a = (object) $b;
	}

	PublicMethod function codeQmBool()
	{
		$a = ($b ? $c : $d);
		$a = (f1() ? f2() : f3());
		$a = ($b ? $c : ($a ? 1 : 2));
		$a = (f1() ? f2() : ($a ? 1 : 2));
		($a = $b) xor $c;
		($a = $b) and $c; // RESULT: ($a = $b) && $c;
		($a = $b) or $c; // RESULT: ($a = $b) || $c;
		$a = $b && $c;
		$a = $b || $c;
	}
#if PHP_VERSION >= 530

	PublicMethod function codeQmDefault()
	{
		$a = $b ?: $d;
		$a = ($b ?: $d) + $c;
		$a = f1() ?: f2();
		$a = ClassName::f1() ?: ClassName::f2();
		$a = ($b ? $c : $d);
		$a = ($b ? $c : $d) + $c;
		$a = (f1() ? f3() : f2());

		if ($b ?: $d) {
			echo 'if ($b ?: $d)';
		}

		if (($b ?: $d) + $c) {
			echo 'if (($b ?: $d) + $c)';
		}

		if (f1() ?: f2()) {
			echo 'if (f1() ?: f2())';
		}
	}
#endif
#if PHP_VERSION >= 500

	public function __destruct()
	{
	}

	/** doc */
	abstract public function abstractMethod($arg);
#endif

#if PHP_VERSION >= 500
	/** doc */
#endif
	PublicMethod function method($a = NULL, $b = NULL)
	{
	}
#if PHP_VERSION >= 500

	/** doc */
	protected function protectedMethod(ClassName $a, $b = array(
		array('array')
		))
	{
		$runtimeArray = array('1');
		$runtimeArray2 = array(
			'1',
			array()
			);
		$runtimeArray3 = array(
			'a' => '1',
			2   => array()
			);
		return 'protected';
	}

	/** doc */
	private function privateMethod(ClassName $a, $b = NULL)
	{
		return 'private';
	}
#endif
}
#if PHP_VERSION >= 500

interface IInterface
{
	public function nothing();
}
#endif

function f1()
{
}

function f2()
{
}

function f3()
{
}

function generator($f)
{
	echo __FUNCTION__;
	echo $f;
#if PHP_VERSION >= 550

	foreach ($a as $b) {
		yield $b;
	}

	yield f1($b);
#endif
}

function simpleCode1()
{
	echo "\r\n";
	echo "\r";
	echo "\n";
	var_dump(array('a' => 'a', 'b' => 'c'), 'b');
	$object = new Child();
	$className = 'ns\\Child';
	$object = new $className();
#if PHP_VERSION >= 500
	$result = $object instanceof Child;
	$cloned = clone $object;
#endif
}
#if PHP_VERSION >= 500

function doTryCatch()
{
	do {
		try {
			echo 'outer try 1';

			try {
				echo 'inner try';
			}
			catch (InnerException $e) {
				echo $e;
			}
			catch (InnerException2 $e2) {
				echo $e2;
			}
#if PHP_VERSION >= 550
			finally {
				echo 'inner finally';
			}
#endif

			echo 'outer try 2';
		}
		catch (OuterException $e) {
			echo $e;
		}
		catch (OuterException2 $e2) {
			echo $e2;
		}
#if PHP_VERSION >= 550
		finally {
			echo 'outer finally';
		}
#endif
	} while (0);
}
#endif

function codeList()
{
	list($a, $b) = array(1, 2);
}

function codeIf()
{
	if ('if()') {
		echo 'if';

		if ('innerIf()') {
			echo 'if innerIf';
		}
	}
	else if ('elseif_()') {
		echo 'else if';

		if ('innerIf_()') {
			echo 'if innerIf';
		}
	}
	else {
		if ('innerIf_()') {
			echo 'if innerIf';
		}

		echo 'else';
	}

	if (!'if(!)') {
		echo 'if(!)';
	}
}

function codeWhile()
{
	while (false) {
		echo 'while';
	}
}

function codeDoWhile()
{
	do {
		echo 'do/while';
	} while (false);
}

function codeFor()
{
	$i = 1;

	for (; $i < 10; ++$i) {
		echo $i;
		break;
	}
}

function codeForeach()
{
	$array = array(
		array('a' => 'b')
		);

	foreach ($array as $value) {
		foreach ($value as $key => $value) {
			echo $key . ' = ' . $value . "\n";
			break 2;
			continue;
		}
	}
}

function codeSwitch()
{
	switch ('$normalSwitch') {
	case 'case1':
		echo 'case1';

		switch ('$nestedSwitch') {
		case 1:
		}

		break;

	case 'case2':
		echo 'case2';
		break;

	default:
		switch ('$nestedSwitch') {
		case 1:
		}

		echo 'default';
		break;
	}

	switch ('$switchWithoutDefault') {
	case 'case1':
		echo 'case1';
		break;

	case 'case2':
		echo 'case2';
		break;
	}

	switch ('$switchWithMiddleDefault') {
	case 'case1':
		echo 'case1';
		break;

	default:
		echo 'default';
		break;

	case 'case2':
		echo 'case2';
		break;
	}

	switch ('$switchWithInitialDefault') {
	default:
		echo 'default';
		break;

	case 'case1':
		echo 'case1';
		break;

	case 'case2':
		echo 'case2';
		break;
	}

	switch (functionCall()) {
	case func1():
		echo 'case1';
		break;

	case func2():
	case func3():
	case 1 + 1:
		echo 'case2';
		break;
	}

	switch (emptySwitch()) {
	}

	switch ('emptySwitch()') {
	}

	switch ('defaultSwitch()') {
	default:
	}
}

function codeDeclare()
{
	declare (ticks=1) {
		echo 1;
	}

	$a = true;

	while ($a) {
		declare (ticks=1) {
			echo 2;
		}

		$a = false;
	}
}

function codeKeyword()
{
	require 'devel/require.php';
	require_once 'devel/require_once.php';
	include 'devel/include.php';
	include_once 'devel/include_once.php';
	echo __FILE__;
#if PHP_VERSION >= 530
	echo __DIR__;
#endif
#if PHP_VERSION >= 530
	echo 'goto a';
	goto a;
	$i = 1;

	for (; $i <= 2; ++$i) {
		goto a;
	}

a:
	echo 'label a';
#endif
	exit();
	exit(1);
	array(eval('array 1'));
	array();
}
#if PHP_VERSION >= 530

function codeLambda()
{
	echo preg_replace_callback('~-([a-z])~', function($match) {
		return strtoupper($match[1]);
	}, 'hello-world');
	$greet = function($name) {
		printf('Hello %s' . "\r\n" . '', $name);
	};
	$greet('World');
	$greet('PHP');
	$total = 0;
	$tax = 1;
	$callback = function($quantity, $product) use($tax, &$total) {
		$tax = 'tax';
		static $static1 = array(1);
		static $static2;
		$tax = 'tax';
		$tax = --$tax;
		$pricePerItem = constant('PRICE_' . strtoupper($product));
		$total += $pricePerItem * $quantity * ($tax + 1);
	};
}
#endif
#if PHP_VERSION >= 500

final class Child extends ClassName implements IInterface
{
	public function __construct()
	{
		parent::__construct('a', 'b');
	}

	public function __destruct()
	{
		parent::__destruct();
	}

	static public function __callStatic($name, $args)
	{
		parent::__callStatic($name, $args);
	}

	public function nothing()
	{
	}

	public function __toString()
	{
		parent::__toString();
	}

	public function __set($name, $value)
	{
	}

	public function __get($name)
	{
	}

	public function __isset($name)
	{
	}

	public function __unset($name)
	{
	}

	public function __sleep()
	{
	}

	public function __wakeup()
	{
	}

	public function __clone()
	{
		return array();
	}
}
#endif

define('CONST_VALUE', 'const value');
#if PHP_VERSION > 530
const CONST_VALUE1 = __FILE__;
const CONST_VALUE2 = __DIR__;
const CONST_VALUE3 = 'a';
#endif
$late = isset($_ENV['LATE']);

if ($late) {
	class LateBindingClass
	{
		PublicMethod function __construct()
		{
		}
	}

	function lateBindingFunction($arg)
	{
		echo 'lateFunction';
		return new lateBindingFunction();
	}
}

?>