php object of class datetime could not be converted to string

Object of class DateTime could not be converted

I can see that there are many questions along these lines but I am very confused as to why the following does not work, taken straight from the PHP docs:

PHP Catchable fatal error: Object of class DateTime could not be converted to string.

In fact every example in the docs gives me this error. Any ideas?

2 Answers 2

You can’t echo the DateTime object directly. You have to use the format method to get the date and / or time part:

If you want to see the details of the object (for debug) you can use var_dump :

php object of class datetime could not be converted to string. Смотреть фото php object of class datetime could not be converted to string. Смотреть картинку php object of class datetime could not be converted to string. Картинка про php object of class datetime could not be converted to string. Фото php object of class datetime could not be converted to string

PHP Catchable fatal error: Object of class DateTime could not be converted to string.

is self-explanatory. The statement:

attempts to print a DateTime object. The echo() language construct expects a string, you pass it a DateTime object. The DateTime class does not implement the __toString() magic method and PHP doesn’t know how to convert a DateTime object to string.

There are so many ways to represent a DateTime object as string and all of them are handled by the DateTime::format() method.

In fact every example in the docs gives me this error.

In fact, every example in the documentation of DateTime::createFromFormat() reads:

Read the documentation of DateTime and DateTime::format() carefully.

Источник

Object could not be converted to string?

Why am I getting this error:

Catchable fatal error: Object of class Card could not be converted to string in /f5/debate/public/Card.php on line 79

All the declarations of Card :

After changing line 79 to this:

I now get this error:

Catchable fatal error: Object of class Author could not be converted to string in /f5/debate/public/Card.php on line 79

php object of class datetime could not be converted to string. Смотреть фото php object of class datetime could not be converted to string. Смотреть картинку php object of class datetime could not be converted to string. Картинка про php object of class datetime could not be converted to string. Фото php object of class datetime could not be converted to string

6 Answers 6

Read about string parsing, you have to enclose the variables with brackets <> :

php object of class datetime could not be converted to string. Смотреть фото php object of class datetime could not be converted to string. Смотреть картинку php object of class datetime could not be converted to string. Картинка про php object of class datetime could not be converted to string. Фото php object of class datetime could not be converted to string

You are trying to echo an object itself, not a string property of it. Check your code carefully.

You probably want to use:

I think one of the object doesn’t have toString() method defined so it cannot be represented as string.

Not the answer you’re looking for? Browse other questions tagged php or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Doctrine: Object of class User could not be converted to string

I keep getting this error with Doctrine:

In my system users can have many permissions in a One to Many relationship. I have set up a User and Permission entity. They look like this (I removed some annotations, getters and setters to reduce clutter):

The problem occurs when I add a new Permission to a User :

This is the last bit of my stack trace:

Any insight would be greatly appreciated.

4 Answers 4

OK. I’ve got it working.

I haven’t fully worked out the reason yet but when I add the following to my User entity it works:

If I find out more I will post here.

Your solution gave me a clue of what is happening.

Even though you have the entities and the anotations, Doctrine is not being able to understand the relation between entities. When doctrine understands the relation between entities, it knows what methods to call (ie User::getId()) but otherwise, it tries to transform whatever you are sending to a scalar value that it can use to query the database. Thats why it is calling the __toString function of the User, and thats why if you return the id in toString, everything works from here.

This is ok, but its a patch, and probably you dont want to keep it if we can find a better solution, since it could be harder to maintain as your application grows.

What i can see, is that in Permissions you have:

You should remove the @ORM\Column(type=»integer» )

About the join columns, it is not mandatory, but you have to be sure that the defauts, are what you want. As we can read here

Before we introduce all the association mappings in detail, you should note that the @JoinColumn and @JoinTable definitions are usually optional and have sensible default values. The defaults for a join column in a one-to-one/many-to-one association is as follows:

so, they will be the same as an explicit:

So it is supposed to look for a column user_id in the Permissions table, and join it with the id column of the User table. We suppose that this is ok.

If this is true, then in your User, the id shouldnt be user_id, but id:

Or if the column name is actually user_id, then the User class is ok, but you have to change the join column to @ORM\JoinColumn(name=»user_id», referencedColumnName=»user_id»)

That much i can say. I cannot try it know, but i will be glad if you can give it a second.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *