If I check for isset($var), I won't be able to differentiate between following 2 cases. In the first one, I am explicitly setting $t1->a to null whereas in the second, it's left unchanged.
<?php
class Test {
public $a;
}
$t1 = new Test();
$t1->a = null;
if(isExplicitlySet($t1->a)) {
echo "yes t1\n";
}
$t2 = new Test();
if(isExplicitlySet($t2->a)) {
echo "yes t2\n";
}
function isExplicitlySet($var) {
//what goes here?
}
Edit : Reason I need this feature is : Before persisting an object of class Test to Database, I need to know if $a was explicitly set to null or was left unchanged. In the later case, I would set it to its default DB value as specified in the table definition.
Ok, since you are writing your own ORM, it might make sense to use the magic methods (as suggested by Machavity). You could create a parent class
abstract class DB_Obj {
protected $attributes = array();
protected $attributes_have_been_set = array();
public function __set($name, $value) {
$this->attributes[$name] = $value;
$this->attributes_have_been_set[$name] = TRUE;
}
public function __get($name) {
return $this->attributes[$name];
}
public function __isset($name) {
return array_key_exists($name, $this->attributes_have_been_set);
}
}
and extend it
class Test extends DB_Obj {
protected $attributes = array(
'a' => NULL
);
}
When you test it now like this, it works properly
$t1 = new Test();
$t1->a = null;
$t2 = new Test();
var_dump( isset($t1->a), isset($t2->a) );
// bool(true) bool(false)
The advantage of this is also, when you want to save it to the db, you don't need to know each attribute's name (or use another function), but can just iterate over the $attributes array.
You can see an answer here Check if value isset and null
By using get_defined_vars
$foo = NULL;
$vars = get_defined_vars();
if (array_key_exists('bar', $vars)) {}; // Should evaluate to FALSE
if (array_key_exists('foo', $vars)) {}; // Should evaluate to TRUE
I would personnally create a class called UntouchedProperty and set my properties to it at instanciation. Then, untouched and set to null will be different.
class UntouchedProperty {}
class Foo
{
public $bar;
public function __construct()
{
$this->bar = new UntouchedProperty;
}
public function wasTouched($property)
{
if ($this->$property instanceof 'UntouchedProperty') {
return false;
}
return true;
}
}
$foo = new Foo;
$foo->wasTouched('bar'); #=> false
$foo->bar = null;
$foo->wasTouched('bar'); #=> true
Related
So I am facing this problem. I have a class representing a record in my database (User in this example). The class has as many properties as the database table has columns. For simplicity, I have just three in my example:
$id - ID of the user (must be set to a positive integer for registered user, might be set to 0 for user objects that aren't saved in the database yet)
$name - Name of user (must be set for every user, but before loading it from the database might be undefined)
$email - E-mail address of the user (might be NULL in case the user didn't submit an e-mail address)
My (simplified) class looks like this:
<?php
class User
{
private $id;
private $name;
private $email;
public function __construct(int $id = 0)
{
if (!empty($id)){ $this->id = $id; }
//If $id === 0, it means that the record represented by this instance isn't saved in the database yet and the property will be filled after calling the save() method
}
public function initialize(string $name = '', $email = '')
{
//If any of the parameters isn't specified, prevent overwriting curent values
if ($name === ''){ $name = $this->name; }
if ($email === ''){ $email = $this->email; }
$this->name = $name;
$this->email = $email;
}
public function load()
{
if (!empty($this->id))
{
//Load name and e-mail from the database and save them into properties
}
}
public function save()
{
if (!empty($this->id))
{
//Update existing user record in the database
}
else
{
//Insert a new record into the table and set $this->id to the ID of the last inserted row
}
}
public function isFullyLoaded()
{
$properties = get_object_vars($this);
foreach ($properties as $property)
{
if (!isset($property)){ return false; } //TODO - REPLACE isset() WITH SOMETHING ELSE
}
return true;
}
//Getters like getName() and getId() would come here
}
Now finally to my problem. As you can see, the instance of this class can be created without all properties set. That's a problem in case I want to e. g. call getName() while the name isn't known yet (it wasn't set via the initialize() method and load() wasn't called). For that, I wrote method isFullyLoaded() which checks if all properties are known and if not, load() should be called (from the method calling isFullyLoaded(). And the core of the problem is, that some variables might be empty strings (''), zero values (0) or even null (like the $email property). So I want to distinguish variables that have any value set (including null) and those who have never been assigned any value.
Specific example: I want to achieve this code:
$user1 = new User(1);
$user1->initialize('Vic', 'nerd.from.klamath#fallout2.com');
var_dump($user1->isFullyLoaded());
$user2 = new User(2);
$user2->initialize('Cassidy', null); //No e-mail was specified during the registration
var_dump($user2->isFullyLoaded());
$user3 = new User(3);
$user3->initialize('Myron'); //E-mail isn't known yet, but might be saved in the database
var_dump($user3->isFullyLoaded());
to output this:
bool(true)
bool(true)
bool(false)
TL:DR How do distinguish undefined variable and variable which has been assigned NULL in PHP?
Here is another way to introduce the custom Undefined class (as singleton). Additionally, be sure that your class properties are typed:
class Undefined
{
private static Undefined $instance;
protected function __constructor()
{
}
protected function __clone()
{
}
public function __wakeup()
{
throw new Exception("Not allowed for a singleton.");
}
static function getInstance(): Undefined
{
return self::$instance ?? (self::$instance = new static());
}
}
class Person
{
private int $age;
public function getAge(): int|Undefined
{
return $this->age ?? Undefined::getInstance();
}
}
$person = new Person();
if ($person->getAge() instanceof Undefined) {
// do something
}
But there is a downside with using singleton pattern, because all the undefined objects in your app will be strictly equal to each other. Otherwise, every get operation returning undefined value will have a side effect namely another piece of allocated RAM.
PHP has not the value undefined like javascript. But it is not strict typed so if you do not find a better solution here is one with an custom type UNDEFINED
<?php
class UNDEFINED { }
class Test {
var $a;
function __construct( $a='' ) {
$this->a = new UNDEFINED();
if( $a !== '' ) {
$this->a = $a;
}
}
function isDefined() {
$result =true;
if(gettype($this->a) === 'object'){
if(get_class($this->a) === 'UNDEFINED') {
$result=false;
}
}
echo gettype($this->a) . get_class($this->a);
return $result;
}
}
$test= new Test();
$test->isDefined();
Here is a may be litte better version which used instanceof instead of get_call and getType
<?php
class UNDEFINED { }
class Test {
var $id;
var $a;
var $b;
function __construct( $id) {
$this->id = $id;
$this->a = new UNDEFINED();
$this->b = new UNDEFINED();
}
function init( $a = '' , $b = '') {
$this->a = $this->setValue($a,$this->a);
$this->b = $this->setValue($b,$this->b);
}
function setValue($a,$default) {
return $a === '' ? $default : $a;
}
function isUndefined($a) {
return $a instanceof UNDEFINED;
}
public function isFullyLoaded()
{
$result = true;
$properties = get_object_vars($this);
print_r($properties);
foreach ($properties as $property){
$result = $result && !$this->isUndefined($property);
if ( !$result) break;
}
return $result;
}
function printStatus() {
if($this->isFullyLoaded() ) {
echo 'Loaded!';
} else {
echo 'Not loaded';
}
}
}
$test= new Test(1);
$test->printStatus();
$test->init('hello');
$test->printStatus();
$test->init('', null);
$test->printStatus();
Use property_exists():
<?php
error_reporting(E_ALL);
// oop:
class A {
public $null_var = null;
}
$a = new A;
if(property_exists($a, 'null_var')) {
echo "null_var property exists\n";
}
if(property_exists($a, 'unset_var')) {
echo "unset_var property exists\n";
}
// procedural:
$null_var = null;
if(array_key_exists('null_var', $GLOBALS)) {
echo "null_var variable exists\n";
}
if(array_key_exists('unset_var', $GLOBALS)) {
echo "unset_var variable exists\n";
}
// output:
// null_var property exists
// null_var variable exists
How can I return null for a property and its children keys that are not set in a class?
I can use __get to make the property to return null but I have no idea how I can do it for its children keys. Any ideas?
class MyClass
{
public $foo;
public $bar;
public function __construct()
{
$this->bar = (object) ['tool' => 'Here we go'];
}
public function __get($name)
{
return (isset($this->$name)) ? $this->$name : null;
}
}
$class1 = new MyClass;
var_dump($class1->foo); // null
var_dump($class1->boo); // null
var_dump($class1->bar->tool); // 'Here we go'
var_dump($class1->bar->mars); // Notice: Undefined property: stdClass::$mars in...
Is it possible to get null for $class1->bar->mars?
Instead of casting $this->bar to (object) which will be an instance of stdClass (which you have no control of), create another class to represent the data you're storing in $this->bar, and define similar __set() and __get() methods.
Example:
class KeyValuePairs {
private $pairs;
public function __construct( $arr = [])
{
$this->pairs = [];
foreach( $arr as $k => $v) {
$this->pairs[$k] = $v;
}
}
public function __get($name)
{
return (isset($this->pairs[$name])) ? $this->pairs[$name] : null;
}
public function __set($name, $value)
{
$this->pairs[$name] = $value;
}
}
Then you can change your __construct() to:
public function __construct()
{
$this->bar = new KeyValuePairs( ['tool' => 'Here we go']);
}
And you get:
$class1 = new MyClass;
var_dump($class1->foo); // null
var_dump($class1->boo); // null
var_dump($class1->bar->tool); // 'Here we go'
var_dump($class1->bar->mars); // null
You cannot magically do this, and I'd suggest it's bad practice to want to write such code. Either you're working with defined data structures, or you're not. If you don't know for sure what your data structure looks like, you shouldn't directly access it like ->foo->bar->baz. Use some code instead with explicit error handling. I'd suggest:
class MyClass {
public $values;
public function __construct() {
$this->values = ['tool' => 'Here we go'];
}
public function get($name) {
return array_reduce(explode('.', $name), function ($value, $key) {
if (is_array($value) && array_key_exists($key, $value)) {
return $value[$key];
}
return null;
}, $this->values);
}
}
$foo = new MyClass;
echo $foo->get('tool');
echo $foo->get('non.existent.key');
Obviously, using clearly defined data structures is the best alternative anyway, then you can be sure those keys exist. If you ever access a non-existent key, PHP will throw an error, as it should.
I want to create properties that are set to mysql data.
class MyClass{
private $a = $r['a'];
private $b = $r['a'];
private $c = $r['c'];
}
I know this is incorrect syntax but I want you to get the idea.
I could create a method that returns a requested mysql data, but I don't want the function to be called for every single row.
You need to implement the magic method __get.
Something like:
class MyClass {
protected $_row = array();
public function __get( $name )
{
if (array_key_exists($name, $this->_row)) {
return $this->_row[$name];
}
return null;
}
public function __isset( $name )
{
return array_key_exists($name, $this->_row);
}
}
And you could used as:
$obj = new MyClass();
$obj->load(); // Or any method to load internal data
echo $obj->a . $obj->b;
Why reinvent the wheel ?
check this mysqli_result::fetch_object
I love the Hash implementation of Ruby where you can initialize the Hash object with a default value. At the moment I'm struggling with implementing a similar object in PHP. This is my first (non-working) shot at this.
class DefaultArray extends ArrayObject {
protected $_defaultValue;
public function setDefault($defaultValue) {
$this->_defaultValue = $defaultValue;
}
public function offsetExists($index) {
return true;
}
public function offsetGet($index) {
if(!parent::offsetExists($index)) {
if(is_object($this->_defaultValue))
$default = clone $this->_defaultValue;
else
$default = $this->_defaultValue;
parent::offsetSet($index, $default);
}
return parent::offsetGet($index);
}
}
$da = new DefaultArray();
assert($da["dummy"] == null);
$da->setDefault = 1;
assert($da["dummy2"] == 1);
The second assertion will fail. Stepping through the code shows that offsetGet is called and the if clause is executed. Nevertheless any array value is null. Any ideas for alternative implementations?
I'm tired of writing
if(!isset($myarr['value']))
$myarr['value'] = new MyObj();
$myarr['value']->myVal=5;
instead of just writing
$myarr['value']->myVal=5;
$da->setDefault(1);
You can also use the __construct magic function:
class DefaultArray extends ArrayObject
{
public function __construct($value = null){
if(is_null($value))
{
$this->value = 'default';
} else {
$this->value = $value;
}
}
}
Try the magic methods __get.
class DefaultArray extends ArrayObject {
protected $_defaultValue;
public function setDefault($defaultValue) {
$this->_defaultValue = $defaultValue;
}
public function __get($index) {
return $this->offsetGet($index);
}
public function offsetGet($index) {
if(!parent::offsetExists($index)) {
if (is_object($this->_defaultValue)) {
$default = clone $this->_defaultValue;
} else {
$default = $this->_defaultValue;
}
parent::offsetSet($index, $default);
}
return parent::offsetGet($index);
}
}
Now you just need to use different keys as the read access will initialize that array items:
$da = new DefaultArray();
assert($da['foo'] == null);
$da->setDefault(1);
assert($da['bar'] == 1);
You could use my tiny library ValueResolver in this case, for example:
class DefaultArray extends ArrayObject
{
public function __construct($value = null){
$this->value = ValueResolver::resolve($value, 'default'); // returns 'default' if $value is null
}
}
and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;
There are also ability to typecasting, for example if your variable's value should be integer, so use this:
$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)
Check the docs for more examples
Why so complicated?
function initVal($value) {
global $myarr;
if(!isset($myarr['value']))
$myarr['value'] = new MyObj();
}
Now you just have to call:
initVal('bla');
$myarr['bla']->bla = 'bla';
But I see, yours is much more neat.
In effect, if I have a class c and instances of $c1 and $c2
which might have different private variable amounts but all their public methods return the same values I would like to be able to check that $c1 == $c2?
Does anyone know an easy way to do this?
You can also implement a equal($other) function like
<?php
class Foo {
public function equals($o) {
return ($o instanceof 'Foo') && $o.firstName()==$this.firstName();
}
}
or use foreach to iterate over the public properties (this behaviour might be overwritten) of one object and compare them to the other object's properties.
<?php
function equalsInSomeWay($a, $b) {
if ( !($b instanceof $a) ) {
return false;
}
foreach($a as $name=>$value) {
if ( !isset($b->$name) || $b->$name!=$value ) {
return false;
}
}
return true;
}
(untested)
or (more or less) the same using the Reflection classes, see http://php.net/manual/en/language.oop5.reflection.php#language.oop5.reflection.reflectionobject
With reflection you might also implement a more duck-typing kind of comparision, if you want to, like "I don't care if it's an instance of or the same class as long as it has the same public methods and they return the 'same' values"
it really depends on how you define "equal".
It's difficult to follow exactly what you're after. Your question seems to imply that these public methods don't require arguments, or that if they did they would be the same arguments.
You could probably get quite far using the inbuilt reflection classes.
Pasted below is a quick test I knocked up to compare the returns of all the public methods of two classes and ensure they were they same. You could easily modify it to ignore non matching public methods (i.e. only check for equality on public methods in class2 which exist in class1). Giving a set of arguments to pass in would be trickier - but could be done with an array of methods names / arguments to call against each class.
Anyway, this may have some bits in it which could be of use to you.
$class1 = new Class1();
$class2 = new Class2();
$class3 = new Class3();
$class4 = new Class4();
$class5 = new Class5();
echo ClassChecker::samePublicMethods($class1,$class2); //should be true
echo ClassChecker::samePublicMethods($class1,$class3); //should be false - different values
echo ClassChecker::samePublicMethods($class1,$class4); //should be false -- class3 contains extra public methods
echo ClassChecker::samePublicMethods($class1,$class5); //should be true -- class5 contains extra private methods
class ClassChecker {
public static function samePublicMethods($class1, $class2) {
$class1methods = array();
$r = new ReflectionClass($class1);
$methods = $r->getMethods();
foreach($methods as $m) {
if ($m->isPublic()) {
#$result = call_user_method($m->getName(), $class1);
$class1methods[$m->getName()] = $result;
}
}
$r = new ReflectionClass($class2);
$methods = $r->getMethods();
foreach($methods as $m) {
//only comparing public methods
if ($m->isPublic()) {
//public method doesn't match method in class1 so return false
if(!isset($class1methods[$m->getName()])) {
return false;
}
//public method of same name doesn't return same value so return false
#$result = call_user_method($m->getName(), $class2);
if ($class1methods[$m->getName()] !== $result) {
return false;
}
}
}
return true;
}
}
class Class1 {
private $b = 'bbb';
public function one() {
return 999;
}
public function two() {
return "bendy";
}
}
class Class2 {
private $a = 'aaa';
public function one() {
return 999;
}
public function two() {
return "bendy";
}
}
class Class3 {
private $c = 'ccc';
public function one() {
return 222;
}
public function two() {
return "bendy";
}
}
class Class4 {
public function one() {
return 999;
}
public function two() {
return "bendy";
}
public function three() {
return true;
}
}
class Class5 {
public function one() {
return 999;
}
public function two() {
return "bendy";
}
private function three() {
return true;
}
}
You can define PHP's __toString magic method inside your class.
For example
class cat {
private $name;
public function __contruct($catname) {
$this->name = $catname;
}
public function __toString() {
return "My name is " . $this->name . "\n";
}
}
$max = new cat('max');
$toby = new cat('toby');
print $max; // echoes 'My name is max'
print $toby; // echoes 'My name is toby'
if($max == $toby) {
echo 'Woohoo!\n';
} else {
echo 'Doh!\n';
}
Then you can use the equality operator to check if both instances are equal or not.
HTH,
Rushi
George: You may have already seen this but it may help: http://usphp.com/manual/en/language.oop5.object-comparison.php
When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.
They don't get implicitly converted to strings.
If you want todo comparison, you will end up modifying your classes. You can also write some method of your own todo comparison using getters & setters
You can try writing a class of your own to plugin and write methods that do comparison based on what you define. For example:
class Validate {
public function validateName($c1, $c2) {
if($c1->FirstName == "foo" && $c2->LastName == "foo") {
return true;
} else if (// someother condition) {
return // someval;
} else {
return false;
}
}
public function validatePhoneNumber($c1, $c2) {
// some code
}
}
This will probably be the only way where you wont have to modify the pre-existing class code