I create a subdomain in Wordpress MU and then when I go to that subdomain, I get this error:
Catchable fatal error: Object of class WP_Error could not be converted
to string in /home/pahouse1/public_html/wp-content/mu-plugins/lifetime
/syncronize.php on line 450**
Did anyone face the same issue? What can I do about this?
A bit more code would be nice.
Normally this means you are trying to print an object. Something like this:
$a = new ObjectA();
echo $a;
Which is not possible because, as the error says, a non-string variable (in this case a class object) couldn't be converted to a string.
You can fix this by writing a magic method __toString() for that class.
Full information can be found at http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring.
Related
I've been working on code that's intended to be used with objects, without really caring what the kind of object is. I wanted to type hint that the method being written expected an object of any type, but ran into some difficulty.
I tried function myFunc (object $obj) and function myFunc (stdClass $obj) but both of these generated errors when I tried to pass objects in:
Catchable fatal error: Argument 1 passed to MyClass::MyFunc() must be an instance of object, instance of ObjectActualClass given
The same happened with stdClass as well
What am I missing? I thought that all classes that didn't explicitly inherit from another class inherited from stdClass, meaning that the base class of every class in PHP would be stdClass. Is this not the case?
stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect.
The best way to enforce this would be to create a degenerate interface called Object. A degenerate interface means it has no defined methods.
interface Object {
// leave blank
}
Then in your base classes, you can implement Object.
class SomeBase implements Object {
// your implementation
}
You can now call your function as you wanted to
function myFunc (Object $obj);
myFunc($someBase);
If you pass any object which inherits from your Object interface, this type hint will pass. If you pass in an array, int, string etc, the type hint will fail.
Well it only took eight years, but this will soon be possible: PHP 7.2 introduces the object type hint! As I write this, it's currently in the RFC stage, and is due to be released in November.
Update, 30th November: PHP 7.2 has been released
RFC: Object typehint
Discussion
This behaves exactly as you might expect:
<?php
class Foo {}
class Bar {}
function takeObject(object $obj) {
var_dump(get_class($obj));
}
takeObject(new Foo);
takeObject(new Bar);
takeObject('not an object');
Will result in:
string(3) "Foo"
string(3) "Bar"
Fatal error: Uncaught TypeError: Argument 1 passed to takeObject() must be an object, string given, called in...
See https://3v4l.org/Svuij
One side-effect of this is that object is now a reserved word, which unfortunately renders #Gaz_Edge's existing solution above broken. Fortunately, all you have to do to fix it is delete the interface.
Although there is no type hinting for objects, you can use:
if (!is_object($arg)) {
return;
}
There is no base class that all objects extend from. You should just remove the typehint and document the expected type in the #param annotation.
There is no built-in mechanism to do this without requiring all users of your interface to extend a specified class. But why would you want to do this anyway? What do all object types have in common that's enough to make them suitable input for your API?
In all probability you wouldn't gain anything even if able to type hint like this. On the other hand, type hinting a parameter to implement an interface (such as Traversable) would be much more meaningful.
If you still want something akin to type hinting, the best you can do is substitute a runtime check with is_object on the parameter.
As of php 7.2 this feature has now been implemented. you can type hint for any object now.
function myFunc(object $myObject): object {
return $myObject;
}
You can review this in the official documentation
Typehint for stdClass works since PHP 5.3+ (if I am not wrong).
Following is valid code using typehint for stdClass construct:
Example test.php:
class Test{
function hello(stdClass $o){
echo $o->name;
}
}
class Arg2 extends stdClass{
public $name = 'John';
function sayHello(){
echo 'Hello world!';
}
}
$Arg1 = new stdClass();
$Arg1->name = 'Peter';
$Arg2 = new Arg2();
$Arg2->sayHello();
$test = new Test();
// OK
$test->hello($Arg1);
$test->hello($Arg2);
// fails
$test->hello(1);
Prints out:
Hello world!
Peter
John
Catchable fatal error: Argument 1 passed to Test::hello() must be an instance of stdClass, integer given, called in test.php on line 32 and defined in test.php on line 5
You could do something like this:
function myFunc ($obj)
{
if ($obj instanceof stdClass) { .... }
}
I found this code, but I'm unsure what it does exactly.
public function attach(IElement $element,IVisitor $colorVis)
{
$element->accept($colorVis);
array_push($this->elements,$element);
}
What does IElement and IVisitor do? Why do they need to be specified there? Thanks!
Type Hinting
$element must be an object and of the class IElement and $colorVis must be an object and of the class IVisitor or a catchable fatal error will be thrown.
So this function definition is defining the types or classes that the arguments must be.
Also, the I prefix is commonly used to mean Interface, but it may not be the case.
I just encountered strange thing going on while working on custom Magento Model.
Model is called as singleton and works:
While trying to clone object property second time it results in fatal error.
function someFunction1() {
$datetime_from = clone $this->from;
}
On the other hand, this function:
function someFunction2() {
echo '<pre>'.__FILE__.':'.__LINE__.'<br>';
var_export(gettype($this->from));
echo '</pre>';
$datetime_from = clone $this->from;
}
results in:
...\Model\Data.php:230
'object'
true
Fatal error: __clone method called on non-object in
Q:\magento\app\code\local\Tece\Goals\Model\Data.php on line 235
Same problem: I was cloning a DateTime object in a loop, debug showed it was fine, but cloning still failed. Turns out the variable got cleared in subsequent iterations, and cloning failed because of that.
However, while researching, I found Do You Clone the DateTime Objects?.
Here's the main point:
Since PHP 5.5 it’s better to use DateTimeImmutable objects whenever it’s possible. They cannot be modified after they are created (they behave like true Value Objects). The methods declared in DateTimeInterface that modify the DateTime objects automatically create and return clones for DateTimeImmutable objects.
That way there's no need to clone at all.
Nevermind this.
The object I was trying to clone was a string, was just other variable, other line number.
Hardest is to find the bug that doesn't exist.
I've been working on code that's intended to be used with objects, without really caring what the kind of object is. I wanted to type hint that the method being written expected an object of any type, but ran into some difficulty.
I tried function myFunc (object $obj) and function myFunc (stdClass $obj) but both of these generated errors when I tried to pass objects in:
Catchable fatal error: Argument 1 passed to MyClass::MyFunc() must be an instance of object, instance of ObjectActualClass given
The same happened with stdClass as well
What am I missing? I thought that all classes that didn't explicitly inherit from another class inherited from stdClass, meaning that the base class of every class in PHP would be stdClass. Is this not the case?
stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect.
The best way to enforce this would be to create a degenerate interface called Object. A degenerate interface means it has no defined methods.
interface Object {
// leave blank
}
Then in your base classes, you can implement Object.
class SomeBase implements Object {
// your implementation
}
You can now call your function as you wanted to
function myFunc (Object $obj);
myFunc($someBase);
If you pass any object which inherits from your Object interface, this type hint will pass. If you pass in an array, int, string etc, the type hint will fail.
Well it only took eight years, but this will soon be possible: PHP 7.2 introduces the object type hint! As I write this, it's currently in the RFC stage, and is due to be released in November.
Update, 30th November: PHP 7.2 has been released
RFC: Object typehint
Discussion
This behaves exactly as you might expect:
<?php
class Foo {}
class Bar {}
function takeObject(object $obj) {
var_dump(get_class($obj));
}
takeObject(new Foo);
takeObject(new Bar);
takeObject('not an object');
Will result in:
string(3) "Foo"
string(3) "Bar"
Fatal error: Uncaught TypeError: Argument 1 passed to takeObject() must be an object, string given, called in...
See https://3v4l.org/Svuij
One side-effect of this is that object is now a reserved word, which unfortunately renders #Gaz_Edge's existing solution above broken. Fortunately, all you have to do to fix it is delete the interface.
Although there is no type hinting for objects, you can use:
if (!is_object($arg)) {
return;
}
There is no base class that all objects extend from. You should just remove the typehint and document the expected type in the #param annotation.
There is no built-in mechanism to do this without requiring all users of your interface to extend a specified class. But why would you want to do this anyway? What do all object types have in common that's enough to make them suitable input for your API?
In all probability you wouldn't gain anything even if able to type hint like this. On the other hand, type hinting a parameter to implement an interface (such as Traversable) would be much more meaningful.
If you still want something akin to type hinting, the best you can do is substitute a runtime check with is_object on the parameter.
As of php 7.2 this feature has now been implemented. you can type hint for any object now.
function myFunc(object $myObject): object {
return $myObject;
}
You can review this in the official documentation
Typehint for stdClass works since PHP 5.3+ (if I am not wrong).
Following is valid code using typehint for stdClass construct:
Example test.php:
class Test{
function hello(stdClass $o){
echo $o->name;
}
}
class Arg2 extends stdClass{
public $name = 'John';
function sayHello(){
echo 'Hello world!';
}
}
$Arg1 = new stdClass();
$Arg1->name = 'Peter';
$Arg2 = new Arg2();
$Arg2->sayHello();
$test = new Test();
// OK
$test->hello($Arg1);
$test->hello($Arg2);
// fails
$test->hello(1);
Prints out:
Hello world!
Peter
John
Catchable fatal error: Argument 1 passed to Test::hello() must be an instance of stdClass, integer given, called in test.php on line 32 and defined in test.php on line 5
You could do something like this:
function myFunc ($obj)
{
if ($obj instanceof stdClass) { .... }
}
I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();