Adjust PHP sync example
Make it work with how globals are actually wrapped. It looks like this example has never been right, but nobody noticed before PHP 8 started warning about the use of unset variables. This reveals that handling of std::string properties needs fixing.
This commit is contained in:
parent
3b63ef27fe
commit
a49cc60772
3 changed files with 48 additions and 16 deletions
|
|
@ -1,13 +1,19 @@
|
|||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
int x = 42;
|
||||
char *s = (char *)"Test";
|
||||
std::string s = "Test";
|
||||
|
||||
void Sync::printer(void) {
|
||||
|
||||
printf("The value of global s is %s\n", s);
|
||||
printf("The value of global x is %d\n", x);
|
||||
printf("The value of class s is %s\n", s);
|
||||
printf("The value of class x is %d\n", x);
|
||||
void Sync::printer() {
|
||||
std::cout << "The value of global s is " << ::s << '\n';
|
||||
std::cout << "The value of global x is " << ::x << '\n';
|
||||
std::cout << "The value of class s is " << this->s << '\n';
|
||||
std::cout << "The value of class x is " << this->x << '\n';
|
||||
}
|
||||
|
||||
void Sync::all_change() {
|
||||
::s = "global change";
|
||||
++::x;
|
||||
this->s = "local change";
|
||||
++this->x;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
extern char *s;
|
||||
#include <string>
|
||||
|
||||
extern std::string s;
|
||||
extern int x;
|
||||
|
||||
class Sync {
|
||||
public:
|
||||
int x;
|
||||
char *s;
|
||||
void printer(void);
|
||||
std::string s;
|
||||
void printer();
|
||||
void all_change();
|
||||
|
||||
Sync() : x(0) { }
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,12 +1,33 @@
|
|||
<?
|
||||
|
||||
// Load module and PHP classes.
|
||||
include("example.php");
|
||||
|
||||
echo "Got new object\n";
|
||||
echo "Got string $s and value $x \n";
|
||||
echo "PHP reading globals: string is '", s_get(), "' and value is ", x_get(), "\n";
|
||||
|
||||
$s = new Sync();
|
||||
$o = new Sync();
|
||||
echo "Got new object\n";
|
||||
|
||||
$s->printer();
|
||||
echo "PHP reading object: string is '", $o->s, "' and value is ", $o->x, "\n";
|
||||
|
||||
$o->printer();
|
||||
|
||||
// FIXME something is up with the setter
|
||||
// type error - s_set("global string");
|
||||
x_set(42);
|
||||
|
||||
// FIXME something is up with the setter
|
||||
// type error - $o->s = "object string";
|
||||
$o->x = 1234;
|
||||
|
||||
echo "PHP reading globals: string is '", s_get(), "' and value is ", x_get(), "\n";
|
||||
echo "PHP reading object: string is '", $o->s, "' and value is ", $s->x, "\n";
|
||||
|
||||
$o->printer();
|
||||
|
||||
echo "Calling all_change() method\n";
|
||||
$o->all_change();
|
||||
|
||||
echo "PHP reading globals: string is '", s_get(), "' and value is ", x_get(), "\n";
|
||||
echo "PHP reading object: string is '", $o->s, "' and value is ", $o->x, "\n";
|
||||
|
||||
$o->printer();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue