Skip to content

promote error "Memcached constructor was not called" to exception #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ static inline php_memc_object_t *php_memc_fetch_object(zend_object *obj) {
php_memc_object_t* intern = NULL; \
php_memc_user_data_t* memc_user_data = NULL;

#if PHP_VERSION_ID < 80000
#define MEMC_METHOD_FETCH_OBJECT \
intern = Z_MEMC_OBJ_P(object); \
if (!intern->memc) { \
Expand All @@ -203,6 +204,16 @@ static inline php_memc_object_t *php_memc_fetch_object(zend_object *obj) {
} \
memc_user_data = (php_memc_user_data_t *) memcached_get_user_data(intern->memc); \
(void)memc_user_data; /* avoid unused variable warning */
#else
#define MEMC_METHOD_FETCH_OBJECT \
intern = Z_MEMC_OBJ_P(object); \
if (!intern->memc) { \
zend_throw_error(NULL, "Memcached constructor was not called"); \
RETURN_THROWS(); \
} \
memc_user_data = (php_memc_user_data_t *) memcached_get_user_data(intern->memc); \
(void)memc_user_data; /* avoid unused variable warning */
#endif

static
zend_bool s_memc_valid_key_binary(zend_string *key)
Expand Down
12 changes: 7 additions & 5 deletions tests/bad_construct_8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ class extended extends Memcached {
}

error_reporting(E_ALL);
$extended = new extended ();
var_dump ($extended->setOption (Memcached::OPT_BINARY_PROTOCOL, true));
try {
$extended = new extended ();
var_dump ($extended->setOption (Memcached::OPT_BINARY_PROTOCOL, true));
} catch (Error $e) {
echo $e->getMessage() . PHP_EOL;
}

echo "OK" . PHP_EOL;

--EXPECTF--
Memcached::__construct(): Argument #1 ($persistent_id) must be of type ?string, stdClass given

Warning: Memcached::setOption(): Memcached constructor was not called in %s
NULL
Memcached constructor was not called
OK