Is there a simple way to remove a member from an object? Not just set it to null, but actually remove it.
Thanks! :)
Edit: I've already tried unset(), and setting the member variable to null obviously doesn't work. I suppose I could convert the object to an array, then remove the array key in question, and convert back to an object, but blech... There's got to be an easier way!
You are using RedBean. Just checked it out. And these bean objects don't have actual properties.
unset($bean->field);
Does not work, because ->field is a virtual attribute. It does not exist in the class. Rather it resides in the protected $bean->properties[] which you cannot access. RedBean only implements the magic methods __get and __set for retrieving and setting attributes.
This is why the unset() does not work. It unsets a property that never existed at that location.
$obj = new stdClass;
$obj->answer = 42;
print_r($obj);
unset($obj->answer);
print_r($obj);
Works fine for me. Are you sure you 're doing it right?
Update:
It also works for properties defined in classes:
class Foo {
public $bar = 42;
}
$obj = new Foo;
print_r($obj);
unset($obj->bar);
print_r($obj);
within you object you can define a magic method called __unset
class Test
{
public $data = array();
public function __unset($key)
{
unset($this->data[$key]);
}
}
And Jon summed up the other factors nicely.
RedBean has a removeProperty method on beans.
Possibly unset().
No you cannot, nor in the Runkit module do I see a way to accomplish that, even if ways to remove methods/functions/constants exist.
With RedBean 4 you can use
unset($bean->someproperty);
Do you want to unset the property merely because you do not want it stored in the database?
If so, just declare the property as private in the class.
Kudos to this answer: Not saving property of a php-redbean to database
Related
I'm trying to build an object in which the properties of the object don't exist directly on the object, but rather are defined through a static variable and stored into a $data property, through the user of __get and __set. It's working out well, until I've gotten to objects. Because of the issue with recursive objects and not having access to create getters and setters on stdClass, I figured internally I can store the values of the object as an array and cast it on output. Unfortunately when someone does something like
$fleet->car->wheels = 4;
I immediately get
Creating default object from empty value
It seems to indicate that it tries to do the outer set (car->wheels) first, then the inner? Which doesn't make sense to me. To me, logically it'd first try to get the value of car from $fleet, then try to set wheels.
Am I thinking about this incorrectly or is this just not possible? I know the other solution is to create an object to hold the values, with getters/setters of it's own, but I'd like to have as little code as possible.
The minimum code to see this:
class Test
{
public function __set($key, $value) {
var_dump($key, $value);
}
}
$test = new Test();
$test->car->wheels = 4;
This was a mistake in logic.
When you set {object}->{key1}->{key2}, you're getting {object}->{key1}, then setting the return of that. So the fact that I was trying to use a setter that wasn't firing was, of course, because a value in {key1} was being set, but not key1 itself.
So adding
public function __get($key) {
$test = new stdClass();
return $test;
}
got rid of the error, because first a new stdClass for car is created, and then 4 is set to wheels.
I apologize if this question doesn't make a lot of sense; I'm having a little bit of trouble putting it into words. I'm still learning a lot about PHP and I don't have the vocabulary down quite yet.
I have a class that has a method that accepts a different class object as it's only parameter:
class myClass {
protected $name;
public function getName() {
return $this->name;
}
public function myMethod($string) {
// Do something
}
}
I would like to be able to do something like this:
$myObject = new myClass();
$myObject->myMethod($myObject->getName());
without using the variable name of the object's method that is being passed in to myMethod(). In my head, that would work something like this:
$myObject->myMethod($this->getName());
but it doesn't. Any suggestions? Is this even possible?
That's not possible because $this exists only withing the class and represents its object. If you have class Person and inside you have $this, that means it is a reference to current Person object. But if you have outside of any class, that represents nothing.
You just cannot. You need to refer object variable explicitly.
You could make your proposition here https://wiki.php.net/rfc/howto to force syntax changing.
Is there a way to get all the methods of an instantiated standard class object? I'm talking about an object that has some methods and properties on it, not a fresh stdClass object. ReflectionClass seems to work only on classes.
Try get_class_methods(), sounds like what you're describing, if I understand you correctly.
You can use get_object_vars to get a list of all of the properties, and then iterate over them (or array_filter them) and determine which of them is_callable:
$myClass = new StdClass;
$myClass->someFunc = function($a) {
return $a - 1;
};
$myClass->someProperty = 42;
$properties = get_object_vars($myClass);
$methods = array_filter($properties, 'is_callable');
I'm not sure if it's my 5.4.39 version of PHP or not, but using the above example and instead doing var_dump(get_class_methods($myClass)); as #Mike suggested returns an empty array.
When I call this function, and add a constructor to my class, all my class properties are already set. How is that possible -- I thought you had to construct a class before you could set its properties?
I guess that PDO uses some internal code to create an object without invoking the constructor. However it is possible to instance a new object without calling the constructor even in pure PHP, all you have to do is to deserialize an empty object:
class SampleClass {
private $fld = 'fldValue';
public function __construct() {
var_dump(__METHOD__);
}
// getters & setters
}
$sc = unserialize(sprintf('O:%d:"%s":0:{}', strlen('SampleClass'), 'SampleClass'));
echo $sc->getFld(); // outputs: fldValue, without calling the construcotr
As of PHP 5.4.0+ ReflectionClass::newInstanceWithoutConstructor() method is available in reflection API.
In php, any array can be cast to an object. My assumption is pdo creates an associative array an then jus casts it. I dont know if he constuctor is called on cast...
Actually, a cast isnt the right word, a conversion occurs behind the scenes. Read this. Blurb on what happens: http://php.net/manual/en/language.types.object.php
now i have class
class test{
var $var_test = 'test';
}
how i can get it
i think
$t = new test();
echo $t->var_test;
is this true
Have you tried it?
But yes, that is true. (It would be better to declare the member with public if you're going to access it from outside the class.)
If your variable is public, yes. Be sure to use proper visibility for your variables and methods.
Typically, you'll want to use get() and set() methods to handle data within the class itself. These keeps people's grubby hands off of your data :) Generally these will return the value from within the class ( return $this->val; ) so nobody is directly able to access the variable.
If you are wondering if
$t = new test();
echo $t->var_test;
is correct, then the answer is yes. I guess that you made the question because you are having problems with code you developed, and you want to understand why it is not working. If that is the case, then you should report the exact code you are using.
As side note, the code you are wrote is for PHP4; PHP5 uses a different syntax for class declaration, even though it is able to parse PHP4 classes.