473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Operator Overloading

I was wondering if there was a way to do operator overlaoding in php
like you can in c++.

In c++ you can do something like:

cout << myobject; -- and have myobject handle the output.

Is there a way to...

echo myobject; -- and be able to have a custom definded method in
myobject do some kind of output?

Any other suggestions?
( I was thinking of having a custom method called echo in myobject so
one could do echo myobject->echo(); This is not as clean....)

Thanks!!

Jul 14 '06 #1
3 5766

deciacco wrote:
I was wondering if there was a way to do operator overlaoding in php
like you can in c++.

In c++ you can do something like:

cout << myobject; -- and have myobject handle the output.

Is there a way to...

echo myobject; -- and be able to have a custom definded method in
myobject do some kind of output?

Any other suggestions?
( I was thinking of having a custom method called echo in myobject so
one could do echo myobject->echo(); This is not as clean....)

Thanks!!
Use the magic method __toString(). Note: it is only invoked when
directly printed. echo "$myobj" will not invoke __toString()

See http://us2.php.net/manual/en/language.oop5.php for more info about
PHP5 objects

Jul 14 '06 #2
deciacco wrote:
Any other suggestions?
( I was thinking of having a custom method called echo in myobject so
one could do echo myobject->echo(); This is not as clean....)

Richard is correct define a function called __toString() in your class
without whatever class variables you want to print off. Notice that
there are 2 underscores on __toString()

You might want to consider an excellent book called PHP 5 Objects,
Patterns, and Practice... I'm reading it now and it covers many of
PHP's advancent features including OO and design patterns.

~john

Jul 14 '06 #3
Thank you both...that's awsome...I will def look into that
book...thanks for the suggestion!!!

~john wrote:
deciacco wrote:
Any other suggestions?
( I was thinking of having a custom method called echo in myobject so
one could do echo myobject->echo(); This is not as clean....)


Richard is correct define a function called __toString() in your class
without whatever class variables you want to print off. Notice that
there are 2 underscores on __toString()

You might want to consider an excellent book called PHP 5 Objects,
Patterns, and Practice... I'm reading it now and it covers many of
PHP's advancent features including OO and design patterns.

~john
Jul 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.