i get a resultset from a class that manages WSDL Data.
I didn't write the code to the class, i only use it.
I call a function to create an ID with the service and want to work with that ID later in the same script.
My Resultset looks like this:
Array
(
[0] => SaveResult Object
(
[id:protected] => newgeneratedID
[success:protected] => 1
)
)
So I tried $response[0]->id to get the ID i need.
Now I get a fatal error.
PHP Fatal error: Cannot access protected property SaveResult::$id
I know it´s a noob question, but I don't get why I can print_r the object but not get the values inside.
You cannot use any of protected data from another space except the same object.
But you can edit the SaveResult object and add getter for id:
public function getId() {
return $this->id;
}
There ought to be a method you can call in the SaveResult class which lets you access the data, something like:
$response[0]->getId();
See the documentation/source code of the class.
You can read a protected property with the ReflectionProperty interface.
The HandyMan component from the phptoolcase library has a handy method to read inaccessible objects properties.
$value = PtcHandyMan::getProperty( $your_object , 'propertyName');
Static property from a singleton class:
$value = PtcHandyMan::getProperty( 'myCLassName' , 'propertyName');
Very simple and usefull, though it is only adviced in few situations as protected/private properties are shouldn't be used outside of their scope.
You can find the HandyMan class here: http://phptoolcase.com/guides/ptc-hm-guide.html
Related
I am having trouble accessing the object property. It should be straightforward, but I am getting
Notice : Undefined property: myClass::$myname:protected in ...
My object, when I do a print_r looks like this:
myClass Object
(
[myname:protected] => Array
(
[key1] => stdClass Object
(
[slug] => name
[name] => Big Name
)
.
.
.
[mykey] => stdClass Object
(...
And I tried printing
print_r($this->{'myname:protected'});
to see if I'll get the array out, from which I can get what I need. But I got the above error.
print_r($this); returns my object, since I am inside a public function (method) in my class myClass.
Why can't I access that key, but when I do print_r($this->mykey->property1) for instance, I can easily get any property for mykey object?
Why is it adding $ in front of the name? I know some oo php, but I'm still a beginner. I have a
protected $myname;
at the beginning of the file, and I'm using it in my __construct() like
public function __construct($myname){...}
Note: I am trying to modify and figure out someone else's code, so I'm not 100% sure what every single piece does yet :\
EDIT:
When I do
print_r($this->myname);
I get the array I need. Can anyone clarify why is that? Thanks.
The member name is myname - the :protected shown in the output of print_r is just debugging information to show that its a protected member of the object.
So from a class method, you would access the member as:
$this->myname
I have a main class that includes component classes, an app class, and a section class.
Everything was fine until I tried to eliminate recursion I found in a print_r of the final $this
Now every class extends from another. Only the components and app class extend from the main class. The section extends from the app class. Which at this point seems fine except for one problem:
When I call a top level function from within an inherited class, the function unsets an important class from the top level parent.
class Framework
{
protected $Data;
function __construct()
{
// include all files...
$this->Data = new MySQLClass;
}
function getItem( $item )
{
$this->Data->table = 'items';
$this->Data->data = array( 'id' => $item );
$this->Data->Query();
}
}
Inside the component class (inherits from main) a call is made to $this->getItem()
It now returns
Fatal error: Call to undefined method stdClass::Query()
I did a print_r of $this->Data at the beginning and end of getItem() and sure enough the lines that define the class action (Data->table, Data->data) are redefining $this->Data
First print_r (normal at this point)
MySQLClass Object ( ... )
Second print_r (changed to values set in function)
stdClass Object
(
[table] => items
[data] => Array
(
[id] => 1
)
)
This function works as intended when called from the main class, and has always worked as intended before the classes were extended. I'm thinking of just holding down CTRL + Z and forgetting I ever noticed. Don't fix what ain't broke right? Still I would like to know exactly why this is happening and how to properly call top level functions from child classes.
Thanks,
This is a syntax question I think... I have an array of classnames, that I use in a factory to generate objects by object type code:
$array = ['a' => '\namespace\AClass', 'b' => '\namespace\BClass'];
I can instantiate these classes from the string name just fine:
$classname = $array['a'];
return new $classname($arg1, $arg2);
What I am trying to do is call a static method of the class named in the array or string, without having to initialize the object - something like:
$classname = $array['a'];
return $classname::formatArg($arg1);
Obviously, this doesn't work since $classname is a string, so how do I tell PHP I am trying to access the object with that name?
Check out this post. How can I call a static method on a variable class?
It look like your code is ok in php 5.3. There's also some ideas how to deal with your problem if you are < 5.3.
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
So by implementing Iterator, ArrayAccess, and Countable built-in interfaces, we have control over what happens inside an object when it's used in foreach loops or if a property is accessed as if it were an array index ($object['id']).
For example, if you wanted, you could set it up so $object['version'] += 1 could automatically increment version field in the database.
What's missing is casting the object to array. Is there any interface or class that allows control over what happens when you do: (array) $object? Any built-in interface or class at all, no matter how obscure? For example: if I wanted (array) $object to return $this->propertyArray instead of the normal object to array conversion of dumping all public object properties?
Note: something like requiring calling $object->toArray() by method name doesn't count, as the idea is to minimize the outside differences between an array and object as much as possible.
no there is not , because toArray() is not an magic function like __toString(); where casting works e.g
$foo = (string) $myObect;
you have to specify toArray() and inside it return your array , may be in future __toArray() might come.
You could add a method like this
public function toArray() {
return get_object_vars( $this );
}
See here. Or check SplFixedArray::toArray.