Skip to content

Commit e8b0883

Browse files
committed
Compatibility with modern PHP 8.x and newer
1 parent bfedf05 commit e8b0883

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

getvar.inc.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,25 @@
1717

1818

1919

20+
////////////////////////////////////////////////////////////////////////////////
21+
// HANDLE PHP VERSION SPECIFIC IMPLEMENTATIONS
22+
////////////////////////////////////////////////////////////////////////////////
23+
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
24+
require_once('modern.php');
25+
} else {
26+
require_once('legacy.php');
27+
}
28+
29+
30+
31+
2032
////////////////////////////////////////////////////////////////////////////////
2133
// THE GETVAR CLASS ITSELF
2234
////////////////////////////////////////////////////////////////////////////////
2335
class getvar implements ArrayAccess {
36+
use getvar_trait;
37+
38+
2439

2540

2641
////////////////////////////////////////////////////////////////////////////
@@ -771,7 +786,7 @@ public function __set($key, $value) {
771786
////////////////////////////////////////////////////////////////////////////
772787
// ARRAYACCESS - WE ARE A READ/DELETE ONLY INSTANCE, THROW EXCEPTION
773788
////////////////////////////////////////////////////////////////////////////
774-
public function offsetSet($key, $value) {
789+
public function _offsetSet($key, $value) {
775790
throw new Exception('Cannot set values on class getvar');
776791
}
777792

@@ -791,7 +806,7 @@ public function __get($key) {
791806
////////////////////////////////////////////////////////////////////////////
792807
// ARRAYACCESS - GET THE $_GET/$_POST VALUE FOR THE GIVEN $KEY
793808
////////////////////////////////////////////////////////////////////////////
794-
public function offsetGet($key) {
809+
public function _offsetGet($key) {
795810
return $this($key);
796811
}
797812

@@ -819,7 +834,7 @@ public function __isset($key) {
819834
////////////////////////////////////////////////////////////////////////////
820835
// ARRAYACCESS - CHECK TO SEE IF $KEY EXISTS IN $_GET/$_POST
821836
////////////////////////////////////////////////////////////////////////////
822-
public function offsetExists($key) {
837+
public function _offsetExists($key) {
823838
return isset($this->{$key});
824839
}
825840

@@ -847,7 +862,7 @@ public function __unset($key) {
847862
////////////////////////////////////////////////////////////////////////////
848863
// ARRAYACCESS - REMOVE A $KEY FROM $_GET/$_POST
849864
////////////////////////////////////////////////////////////////////////////
850-
public function offsetUnset($key) {
865+
public function _offsetUnset($key) {
851866
unset($this->{$key});
852867
}
853868

legacy.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
trait getvar_trait {
4+
5+
6+
////////////////////////////////////////////////////////////////////////////
7+
// ARRAYACCESS - CHECK TO SEE IF $KEY EXISTS IN $_GET/$_POST
8+
////////////////////////////////////////////////////////////////////////////
9+
public function offsetExists($key) {
10+
return $this->_offsetExists($key);
11+
}
12+
13+
14+
15+
16+
////////////////////////////////////////////////////////////////////////////
17+
// ARRAYACCESS - GET THE $_GET/$_POST VALUE FOR THE GIVEN $KEY
18+
////////////////////////////////////////////////////////////////////////////
19+
public function offsetGet($key) {
20+
return $this->_offsetGet($key);
21+
}
22+
23+
24+
25+
26+
////////////////////////////////////////////////////////////////////////////
27+
// ARRAYACCESS - WE ARE A READ/DELETE ONLY INSTANCE, THROW EXCEPTION
28+
////////////////////////////////////////////////////////////////////////////
29+
public function offsetSet($key, $value) {
30+
$this->_offsetSet($key, $value);
31+
}
32+
33+
34+
35+
36+
////////////////////////////////////////////////////////////////////////////
37+
// ARRAYACCESS - REMOVE A $KEY FROM $_GET/$_POST
38+
////////////////////////////////////////////////////////////////////////////
39+
public function offsetUnset($key) {
40+
$this->_offsetUnset($key);
41+
}
42+
}

modern.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
trait getvar_trait {
4+
5+
6+
////////////////////////////////////////////////////////////////////////////
7+
// ARRAYACCESS - CHECK TO SEE IF $KEY EXISTS IN $_GET/$_POST
8+
////////////////////////////////////////////////////////////////////////////
9+
public function offsetExists($key) : bool {
10+
return $this->_offsetExists($key);
11+
}
12+
13+
14+
15+
16+
////////////////////////////////////////////////////////////////////////////
17+
// ARRAYACCESS - GET THE $_GET/$_POST VALUE FOR THE GIVEN $KEY
18+
////////////////////////////////////////////////////////////////////////////
19+
public function offsetGet($key) : mixed {
20+
return $this->_offsetGet($key);
21+
}
22+
23+
24+
25+
26+
////////////////////////////////////////////////////////////////////////////
27+
// ARRAYACCESS - WE ARE A READ/DELETE ONLY INSTANCE, THROW EXCEPTION
28+
////////////////////////////////////////////////////////////////////////////
29+
public function offsetSet($key, $value) : void {
30+
$this->_offsetSet($key, $value);
31+
}
32+
33+
34+
35+
36+
////////////////////////////////////////////////////////////////////////////
37+
// ARRAYACCESS - REMOVE A $KEY FROM $_GET/$_POST
38+
////////////////////////////////////////////////////////////////////////////
39+
public function offsetUnset($key) : void {
40+
$this->_offsetUnset($key);
41+
}
42+
}

0 commit comments

Comments
 (0)