php - The in_array doesn't compare my converted objects as it is supposed to -
i'm developping website, if user changes data, should stored on background, see did last change , etc... . have 1 object called event, data onscreen devided 2 tabs (client , event). after submit, fields , put data in object. have self made function compare values in new boject values of old object:
function createarrayreturndiff($obj1, $obj2) { $helparray1 = (array) $obj1; //convert object array $helparray2 = (array) $obj2; //convert object array $help = array_diff_assoc($helparray2, $helparray1); //computes difference of arrays additional index check return $help; }
now works fine, array returned names of field , new value. here comes tricky part. after return of array, loop trough want check tab value on in order give beter user feedback later. if value on cleint or event tab. made 2 arrays describe fields in each tab.
$tabklant = array('evenementfirmanaam', 'evenementaanspreking', 'evenementcontactpersoon', 'evenementcontactpersoonstraat', 'evenementcontactpersoongemeente', 'evenementcontactpersoonland', 'evenementcontactpersoonmail', 'evenementcontactpersoontel', 'evenementgeldigheidsdatum', 'evenementfacturatiegegevens', 'evenementfactuur_mededeling', 'evenementbestelbon', 'evenementreferentie'); $tabevenement = array('evenementstartdatum', 'evenementeinddatum', 'evenementnaam', 'evenementfeestlocatie', 'evenementcontactfeestlocatie', 'evenementaantal', 'evenementact_speeches_opm', 'evenementdj', 'evenementinleiding');
now code check:
foreach ($help $key => $value) { if (in_array($key, $tabevent)) { $tab = "event"; } else if (in_array($key, $tabclient)) { $tab = "client"; } else { $tab = ""; } }
now tried change evenementfirmanaam, $help array contains values key = evenementfirmanaam , value = 'xxxx'. looks supposed work. reason, can't find value in in_array of foreach.
after tried write away data database. used mysqli_real_escape_string on $key of array (firmanaam in case) , found out creating string like: '\0evenement\0firmanaam' . have no idea why \0 added, have feeling reason why in_array function won't compare values properly. have idea problem might be?
the problem firmanaam
property of evenement
class (which $obj1
, $obj2
instances of) private, results in cast array creating special keys:
if object converted array, result array elements object's properties. keys member variable names, few notable exceptions: integer properties unaccessible; private variables have class name prepended variable name; protected variables have
'*'
prepended variable name. these prepended values have null bytes on either side. can result in unexpected behaviour.
in essence, being punished violating logical design of class: if $firmanaam
private
outside world should not have access value. cast array allow value should not this.
since using evenement
encapsulate , hide data members, way. if want access members, provide , use getter. if want compare 2 instances specific semantics, add comparison method class.
Comments
Post a Comment