Exception instead Notice - php

I have two classes:
class Test {
public $name;
}
/*******/
class MyClass {
private $_test = NULL;
__get($name)
{
return $this->$name;
}
__set($name,$value)
{
$this->$name = $value;
}
}
And when I want to use it this way:
$obj1 = new MyClass();
$obj1->_test = new Test();
$obj1->_test->name = 'Test!';
Everything is ok. But it is also possible, that someone might use it this way:
$obj1 = new MyClass();
$obj1->_test->name = 'Test!';
Then I get notice "Notice: Indirect modification of overloaded property MyClass::$_test has no effect in /not_important_path_here". Instead that notice I want to throw an Exception. How to do it?

What you're looking for is the ErrorException object.
function exception_error_handler($no, $str, $file, $line ) { throw new ErrorException($str, 0, $no, $file, $line); }
set_error_handler("exception_error_handler");

In your __get() method check to see if $_test is an instance of Test. If it is return it, otherwise throw the exception in the __get() method.

Related

PHP 8: is it possible to change a class outside the class? [duplicate]

How can I create a property from a given argument inside a object's method?
class Foo{
public function createProperty($var_name, $val){
// here how can I create a property named "$var_name"
// that takes $val as value?
}
}
And I want to be able to access the property like:
$object = new Foo();
$object->createProperty('hello', 'Hiiiiiiiiiiiiiiii');
echo $object->hello;
Also is it possible that I could make the property public/protected/private ? I know that in this case it should be public, but I may want to add some magik methods to get protected properties and stuff :)
I think I found a solution:
protected $user_properties = array();
public function createProperty($var_name, $val){
$this->user_properties[$var_name] = $val;
}
public function __get($name){
if(isset($this->user_properties[$name])
return $this->user_properties[$name];
}
do you think it's a good idea?
There are two methods to doing it.
One, you can directly create property dynamically from outside the class:
class Foo{
}
$foo = new Foo();
$foo->hello = 'Something';
Or if you wish to create property through your createProperty method:
class Foo{
public function createProperty($name, $value){
$this->{$name} = $value;
}
}
$foo = new Foo();
$foo->createProperty('hello', 'something');
The following example is for those who do not want to declare an entire class.
$test = (object) [];
$prop = 'hello';
$test->{$prop} = 'Hiiiiiiiiiiiiiiii';
echo $test->hello; // prints Hiiiiiiiiiiiiiiii
Property overloading is very slow. If you can, try to avoid it. Also important is to implement the other two magic methods:
__isset();
__unset();
If you don't want to find some common mistakes later on when using these object "attributes"
Here are some examples:
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
EDITED after Alex comment:
You can check yourself the differences in time between both solutions (change $REPEAT_PLEASE)
<?php
$REPEAT_PLEASE=500000;
class a {}
$time = time();
$a = new a();
for($i=0;$i<$REPEAT_PLEASE;$i++)
{
$a->data = 'hi';
$a->data = 'bye'.$a->data;
}
echo '"NORMAL" TIME: '.(time()-$time)."\n";
class b
{
function __set($name,$value)
{
$this->d[$name] = $value;
}
function __get($name)
{
return $this->d[$name];
}
}
$time=time();
$a = new b();
for($i=0;$i<$REPEAT_PLEASE;$i++)
{
$a->data = 'hi';
//echo $a->data;
$a->data = 'bye'.$a->data;
}
echo "TIME OVERLOADING: ".(time()-$time)."\n";
Use the syntax: $object->{$property}
where $property is a string variable and
$object can be this if it is inside the class or any instance object
Live example: http://sandbox.onlinephpfunctions.com/code/108f0ca2bef5cf4af8225d6a6ff11dfd0741757f
class Test{
public function createProperty($propertyName, $propertyValue){
$this->{$propertyName} = $propertyValue;
}
}
$test = new Test();
$test->createProperty('property1', '50');
echo $test->property1;
Result: 50

Don't allow to dynamically create public properties

Let's start with simple PHP code:
<?php
class Test
{
}
$test = new Test();
$test->x = 20;
echo $test->x;
The problem here is this code works without any problem (tested in PHP 7.1, probably in some previous versions it works also without any problems).
The problem I see here that when using code like this, it's very easy to write code that is very hard to analyse and can contain some hidden bugs.
The question: is there any way to don't allow to dynamically create properties for objects especially outside of the class?
For custom class like this the solution that could be used is creating custom __set method that will creating not declared properties like this:
public function __set($property, $value)
{
if (!property_exists($this, $property)) {
throw new Exception("Property {$property} does not exit");
}
$this->$property = $value;
}
but it obviously doesn't solve problem of protected/private properties - property_exists would return true also for protected and private properties (need to use Reflection for this).
<?php
ini_set("display_errors", 1);
class Test
{
protected $name="ss";
public function __set($property, $value)
{
//Checked for undefined properties
if(!isset(get_object_vars($this)[$property]))
{
throw new Exception("Property {$property} does not exit");
}
//Checking for public properties
$prop = new ReflectionProperty($this, "name");
if(!$prop->isPublic())
{
throw new Exception("Property {$property} does not exit");
}
//Checking for non-existing properties
if (!property_exists($this, $property))
{
throw new Exception("Property {$property} does not exit");
}
$this->$property = $value;
}
}
$test = new Test();
$test->x = 20;
echo $test->x;

How to use variable name to call a class?

I want to use a variable (string value) to call a Class. Can I do it ? I search for PHP ReflectionClass but I do not know how to use a method from Reflection Result. Like this:
foreach($menuTypes as $key => $type){
if($key != 'Link'){
$class = new \ReflectionClass('\App\Models\\' . $key);
//Now $class is a ReflectionClass Object
//Example: $key now is "Product"
//I'm fail here and cannot call the method get() of
//the class Product
$data[strtolower($key) . '._items'] = $class->get();
}
}
Without ReflectionClass:
$instance = new $className();
With ReflectionClass: use the ReflectionClass::newInstance() method:
$instance = (new \ReflectionClass($className))->newInstance();
I found one like this
$str = "ClassName";
$class = $str;
$object = new $class();
You can use directly like below
$class = new $key();
$data[strtolower($key) . '._items'] = $class->get();
The risk is that the class doesn't exist. So it's better to check before instantiating.
With php's class_exists method
Php has a built-in method to check if the class exists.
$className = 'Foo';
if (!class_exists($className)) {
throw new Exception('Class does not exist');
}
$foo = new $className;
With try/catch with rethrow
A nice approach is trying and catching if it goes wrong.
$className = 'Foo';
try {
$foo = new $className;
}
catch (Exception $e) {
throw new MyClassNotFoundException($e);
}
$foo->bar();

How to simulate non existing method error in __call?

Maybe strange question, but...
I have a magic __call method, that return instances of certain classes, or, if there are no such class, calls same method in underlying object.
public function __call($name, $arguments)
{
$class = 'My\\Namespace\\' . $name;
if (class_exists($class, true)) {
$reflect = new \ReflectionClass($class);
return $reflect->newInstanceArgs($arguments);
} elseif (is_callable([$this->connector, $name])) {
return call_user_func_array([&$this->connector, $name], $arguments);
} else {
// ????
}
}
But what to do in else block? Can I simulate undefined method error? Or what exception to throw would be correct?
You can trigger PHP errors manually using trigger_error:
trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);
See http://php.net/manual/en/function.trigger-error.php

Override __set magic function in php

I'm trying to create a method that will allow me to set properties within a class using the setVal() function, if the user is trying to set the value from outside the class without using the 'forceSet' function then it will throw an exception.
The problem is that its throwing an exception even if the $forceSet is true. If i set the property manually in the class to have private access then everything works fine, but this is not an option as I wish to be able to set various properties in this class dynamically.
class test
{
private $_allowedCols = array('title', 'name', 'surname');
public function __set($n,$v)
{
$this->setVal($n, $v);
}
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet && !in_array($this->_allowedCols, $name))
{
throw new Exception('cant set value');
}
$this->$name = $value;
}
}
$b = new test;
$b->setVal('blah', 'test', true);
print_r($b);
exit;
What I want to be able to do is set all the values from a $_POST into properties in the object. I want to check against the $_allowedCols to make sure only values I want are being put into the object but sometimes I might want to force values in from the code that aren't in the $_allowedCols.
Any ideas?
The hacks will work but it might be cleaner to use an internal array. Something like:
class test
{
private $data = array();
public function __set($n,$v)
{
if (isset($this->data[$n])) return $this->data[$n] = $v;
throw new Exception('cant set value');
}
public function __get($n)
{
if (isset($this->data[$n])) return $this->data[$n];
throw new Exception('cant retrieve value');
}
public function setVal($name, $value)
{
$this->data[$name] = $value;
}
}
But if you want to stick with your approach then:
class test
{
private $forceFlag = false;
public function __set($name,$value)
{
if ($this->forceFlag) return $this->$name = $value;
throw new Exception('cant set value');
}
public function setVal($name, $value)
{
$this->forceFlag = true;
$this->$name = $value;
$this->forceFlag = false;
}
}
If you look at the stack trace of your exception, you'll notice the call to set __set is being triggered by this line:
$this->$name = $value;
Then in __set, it does $this->setVal($n, $v), which uses the default value of false, and thus throws the exception. To fix this, you can modify your call in __set to be:
$this->setVal($n, $v, true);
With the above code, this line:
$this->$name = $value;
...invokes:
test::__set('blah', 'test');
...because test::$blah is undefined, which in turn invokes:
test::setVal('blah', 'test', false);
A possible, yet not perfect, workaround is this:
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet && isset($value))
{
throw new Exception('cant set value');
}
$this->$name = null;
$this->$name = $value;
}
Although I'm not sure what the point of your code is.
It looks like you write much code for a functionality PHP offers out of the box:
$b = new test;
$b->blah = 'test';
print_r($b);
You don't need __set for this, nor the setVal(ue) function.
However when you want to control the access, you need to ensure that you're not binding it to members. Instead store it inside of a map as a private member:
class test
{
private $values;
public function __set($n,$v)
{
$this->setVal($n, $v);
}
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet)
{
throw new Exception('cant set value');
}
$this->values[$name] = $value;
}
}
This ensures, that a member exists that is set, so that __set is not triggered again.
After testing so many options .. the is the one that works the best for me
I chose this because
Use of Exception terminates the entire scripts or one has to catch exception anything time a value is declared
__set and __get can easily be overriding by extending class
Implementation that can be used with multiple class
What to be able to use the Object directly without having to add another getter method
Locking can cause conflict
The script would not change your existing application structure
Can be used with Singleton ..
Code :
abstract class Hashtable
{
final $hashTable = array() ;
final function __set($n,$v)
{
return false ;
}
final function __get($n)
{
return #$this->hashTable[$n] ;
}
final function _set($n, $v)
{
$this->hashTable[$n] = $v ;
}
}
class Test extends Hashtable {} ;
$b = new Test();
$b->_set("bar","foo",true);
$b->_set("hello","world",true);
//$b->setVal("very","bad"); // false
$b->bar = "fail" ;
var_dump($b,$b->bar);
Output
object(Test)[1]
public 'hashTable' =>
array
'bar' => string 'foo' (length=3)
'hello' => string 'world' (length=5)
string 'foo' (length=3)
I hope this helps
Thanks
:)

Categories