473,836 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Oriented PHP

I am new to PHP, just one day new. But I am otherwise a seasoned
programmer on many other languages like C, VB 6, C#, VB.NET, Win32
platform and have some tiny bit experience in MFC, C++, Python.

All the tutorials I read so far about PHP contain procedural examples.
I haven't yet come accross a tutorial on the object oriented PHP. Could
someone point me to one?

Jun 17 '06
73 4522
Tony Marston wrote:
"David Haynes" <da***********@ sympatico.ca> wrote in message
news:Tv******** ***********@fe2 6.usenetserver. com...
Tony Marston wrote:
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
Actually, things like private declarations are very important. They are
unimportant only to those who don't understand OO programming - or those
who are sloppy in their implementations .
You don't *need* private declarations. Code will work just as well
without them.

What the heck! Let's just throw away multiply and divide. We don't *need*
them. We can just use add and subtract.

Heck! Why do we need subtract? We can just add negative values.

Hey! Assembler works well without private declarations. Let's just pitch
PHP all together.

The point is that constructs are added to language to assist in the
functionality and maintainability of programs written in that language.
The introduction of private and protected adds to the maintainability both
through its inherent protection capabilities and its clarity of intent for
the maintainer.

Now if PHP could just get polymorphism...


OOP with PHP already has polymorphism. You obviously don't know what
polymorphism means.

Tony:

PHP5 has weak polymorphism but not true polymorphism in the sense that
it is used in OOP.

You cannot, for instance, have multiple constructors for an object where
the number of the arguments to the constructor are variable. Yes, you
may supply default values to 'optional' arguments but consider this example:

class Foo {
function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor. Nor could I define multiple __constructor() methods
to allow for the new Foo($one, $three) case.

This type of action is common in other OOP languages such as Java and C++.

To add to the confusion from an OOP programmer's viewpoint, in order to
invoke the 'default' value for $three, I *must* supply a value for $two.
That is, new Foo($one, , $three) is syntactically illegal and there is
AFAIK no way to specify that we want $two to take its default value
while supplying a value for $three.

So, maybe its not me who is unclear on the concept of polymorphism...

-david-

Jun 25 '06 #21
On 2006-06-25, David Haynes <da***********@ sympatico.ca> wrote:
PHP5 has weak polymorphism but not true polymorphism in the sense that
it is used in OOP.
So how would you define polymorphism? And what exactly are the
differences between 'weak' and 'true' polymorphism?

If i look at http://en.wikipedia.org/wiki/Polymor...ter_science%29

"The concept of polymorphism applies to data types in addition to
functions. A function that can evaluate to and be applied to values of
different types is known as a polymorphic function. A data type that
contains elements of different types is known as a polymorphic data
type."
function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor.


Imho that's the same as saying: I'm standing with my back against a wall,
and now i'm wondering why i can't step backwards anymore...

Define your constructor as __constructor($ args) and handle with
func_num_args and func_get_args(s ) any number of parameters...

(I do agree that the language/compiler can, probably should, make this
easier... But that's a different discussion.)

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be >
Jun 25 '06 #22
Tony Marston wrote:
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:4K******** *************** *******@comcast .com...
David Haynes wrote:
Jerry Stuckle wrote:


< snip >
I really don't know how hard it would be to add - but I would like to see
it added. One thing I don't like all that much is the untyped variables.
It makes things "easy" - but leads to some sloppy programming.

It also means you can't overload functions - at least not with the same
number of parms. Typing the constructor arguments is a good start. But
expanding that to other functions without breaking existing code might be
more difficult.

You don't need function overloading in PHP. Why? First you need to look at
why other languages need it:
(a) To accept arguments of different types.
(b) To make some arguments optional.

With PHP you can accept an argument of any type and then cast it to the one
you want, so item (a) is irrelevant.

With PHP you can define a default value for an argument if it is not
supplied, so item (b) is irrelevant.


Sure. I would love to be able to have a function which does different things
based on the type of parameter passed. It would save a lot of work trying to
sort out parameters and potentially having to change existing code when adding
new parameter types.

But we already know you don't understand or appreciate real OO programming,
Tony. So quite frankly, I don't give a damn what you think.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 25 '06 #23
Tony Marston wrote:
"David Haynes" <da***********@ sympatico.ca> wrote in message
news:Tv******** ***********@fe2 6.usenetserver. com...
Tony Marston wrote:
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message

Actually, things like private declarations are very important. They are
unimporta nt only to those who don't understand OO programming - or those
who are sloppy in their implementations .

You don't *need* private declarations. Code will work just as well
without them.


What the heck! Let's just throw away multiply and divide. We don't *need*
them. We can just use add and subtract.

Heck! Why do we need subtract? We can just add negative values.

Hey! Assembler works well without private declarations. Let's just pitch
PHP all together.

The point is that constructs are added to language to assist in the
functionali ty and maintainability of programs written in that language.
The introduction of private and protected adds to the maintainability both
through its inherent protection capabilities and its clarity of intent for
the maintainer.

Now if PHP could just get polymorphism...

OOP with PHP already has polymorphism. You obviously don't know what
polymorphism means.


No, Tony, you really don't know what polymorphism is, do you?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 25 '06 #24
Tim Van Wassenhove wrote:
On 2006-06-25, David Haynes <da***********@ sympatico.ca> wrote:
PHP5 has weak polymorphism but not true polymorphism in the sense that
it is used in OOP.


So how would you define polymorphism? And what exactly are the
differences between 'weak' and 'true' polymorphism?

If i look at http://en.wikipedia.org/wiki/Polymor...ter_science%29

"The concept of polymorphism applies to data types in addition to
functions. A function that can evaluate to and be applied to values of
different types is known as a polymorphic function. A data type that
contains elements of different types is known as a polymorphic data
type."


Keep reading the article you referenced.

"This type of polymorphism [overloading] is common in object-oriented
programming languages, many of which allow operators to be overloaded in
a manner similar to functions (see operator overloading). It is also
used extensively in the purely functional programming language Haskell
in the form of type classes. Many languages lacking ad-hoc polymorphism
suffer from long-winded names such as print_int, print_string, etc. (see
C, Objective Caml)."
function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor.


Imho that's the same as saying: I'm standing with my back against a wall,
and now i'm wondering why i can't step backwards anymore...

Define your constructor as __constructor($ args) and handle with
func_num_args and func_get_args(s ) any number of parameters...

(I do agree that the language/compiler can, probably should, make this
easier... But that's a different discussion.)


What you have specified is a non-overloaded generic constructor. I view
this as a work-around due to the inability of the PHP interpreter to
handle overloading polymorphism.

I'm not saying that this is a bad thing - in fact if you rewind your
news feed about a week - you will see that I recommended such an
approach to another coder, but I am saying that most strongly-typed OO
languages do support overloading as part of the their OOP environment.

-david-
Jun 25 '06 #25
Tim Van Wassenhove wrote:
On 2006-06-25, David Haynes <da***********@ sympatico.ca> wrote:
PHP5 has weak polymorphism but not true polymorphism in the sense that
it is used in OOP.

So how would you define polymorphism? And what exactly are the
differences between 'weak' and 'true' polymorphism?

If i look at http://en.wikipedia.org/wiki/Polymor...ter_science%29

"The concept of polymorphism applies to data types in addition to
functions. A function that can evaluate to and be applied to values of
different types is known as a polymorphic function. A data type that
contains elements of different types is known as a polymorphic data
type."

function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor.

Imho that's the same as saying: I'm standing with my back against a wall,
and now i'm wondering why i can't step backwards anymore...

Define your constructor as __constructor($ args) and handle with
func_num_args and func_get_args(s ) any number of parameters...

(I do agree that the language/compiler can, probably should, make this
easier... But that's a different discussion.)


The constructor overloading example isn't really polymorphism. It's just
function overloading.

Polymorphism is the ability to operate on objects of derived classes, without
knowing what the derived classes are (or potentially even the existence of the
derived classes).

For instance - let's say we have class "mammal" with function "eats". Now all
mammals eat something, but what the class of mammals eats is not defines.

So, derive from mammal the class "ape" and have eats return "bananas". Also
derive the class "horse" and have eats return "oats".

Now - when you create an object of the class "ape" you can pass it to a function
which takes a "mammal". And you can print out what *this* mammal eats. The
same with a horse.

Polymorphism is closely tied to inheritance. But while inheritance allows the
programmer to take advantage of the commonalities between classes, polymorphism
allows the program to take advantage of the differences.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 25 '06 #26

"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:l7******** *************** *******@comcast .com...
Tony Marston wrote:
"David Haynes" <da***********@ sympatico.ca> wrote in message
news:Tv******** ***********@fe2 6.usenetserver. com...
Tony Marston wrote:

"Jerry Stuckle" <js*******@attg lobal.net> wrote in message

>Actually , things like private declarations are very important. They
>are unimportant only to those who don't understand OO programming - or
>those who are sloppy in their implementations .

You don't *need* private declarations. Code will work just as well
without them.
What the heck! Let's just throw away multiply and divide. We don't *need*
them. We can just use add and subtract.

Heck! Why do we need subtract? We can just add negative values.

Hey! Assembler works well without private declarations. Let's just pitch
PHP all together.

The point is that constructs are added to language to assist in the
functionalit y and maintainability of programs written in that language.
The introduction of private and protected adds to the maintainability
both through its inherent protection capabilities and its clarity of
intent for the maintainer.

Now if PHP could just get polymorphism...

OOP with PHP already has polymorphism. You obviously don't know what
polymorphism means.


No, Tony, you really don't know what polymorphism is, do you?


The simplest definition of polymorphism is "same interface, different
implementation" . This means that different objects can share the same
interface but which do different things. What do you think it means?

--
Tony Marston

http://www.tonymarston.net
http://www.radicore.org

Jun 25 '06 #27

"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:l7******** *************** *******@comcast .com...
Tony Marston wrote:
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:4K******** *************** *******@comcast .com...
David Haynes wrote:

Jerry Stuckle wrote:


< snip >
I really don't know how hard it would be to add - but I would like to see
it added. One thing I don't like all that much is the untyped variables.
It makes things "easy" - but leads to some sloppy programming.

It also means you can't overload functions - at least not with the same
number of parms. Typing the constructor arguments is a good start. But
expanding that to other functions without breaking existing code might be
more difficult.

You don't need function overloading in PHP. Why? First you need to look
at why other languages need it:
(a) To accept arguments of different types.
(b) To make some arguments optional.

With PHP you can accept an argument of any type and then cast it to the
one you want, so item (a) is irrelevant.

With PHP you can define a default value for an argument if it is not
supplied, so item (b) is irrelevant.


Sure. I would love to be able to have a function which does different
things based on the type of parameter passed. It would save a lot of work
trying to sort out parameters and potentially having to change existing
code when adding new parameter types.

But we already know you don't understand or appreciate real OO
programming, Tony. So quite frankly, I don't give a damn what you think.


Just because my understanding and implementation of OOP is different from
yours does not make you right and me wrong.

--
Tony Marston

http://www.tonymarston.net
http://www.radicore.org

Jun 25 '06 #28

"David Haynes" <da***********@ sympatico.ca> wrote in message
news:wa******** ***********@fe0 3.usenetserver. com...
Tim Van Wassenhove wrote:
On 2006-06-25, David Haynes <da***********@ sympatico.ca> wrote:
PHP5 has weak polymorphism but not true polymorphism in the sense that
it is used in OOP.


So how would you define polymorphism? And what exactly are the
differences between 'weak' and 'true' polymorphism?

If i look at
http://en.wikipedia.org/wiki/Polymor...ter_science%29

"The concept of polymorphism applies to data types in addition to
functions. A function that can evaluate to and be applied to values of
different types is known as a polymorphic function. A data type that
contains elements of different types is known as a polymorphic data
type."


Keep reading the article you referenced.

"This type of polymorphism [overloading] is common in object-oriented
programming languages, many of which allow operators to be overloaded in a
manner similar to functions (see operator overloading). It is also used
extensively in the purely functional programming language Haskell in the
form of type classes. Many languages lacking ad-hoc polymorphism suffer
from long-winded names such as print_int, print_string, etc. (see C,
Objective Caml)."
function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor.


Imho that's the same as saying: I'm standing with my back against a wall,
and now i'm wondering why i can't step backwards anymore...

Define your constructor as __constructor($ args) and handle with
func_num_args and func_get_args(s ) any number of parameters...

(I do agree that the language/compiler can, probably should, make this
easier... But that's a different discussion.)


What you have specified is a non-overloaded generic constructor. I view
this as a work-around due to the inability of the PHP interpreter to
handle overloading polymorphism.


Just because PHP does not handle overloading in the way that other languages
do does not mean that it does not support polymorphism. Overloading is not
the same as polymorphism. They are different concepts that may or may not be
employed at the same time.

--
Tony Marston

http://www.tonymarston.net
http://www.radicore.org

Jun 25 '06 #29

"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:SY******** *************** *******@comcast .com...
Tim Van Wassenhove wrote:
On 2006-06-25, David Haynes <da***********@ sympatico.ca> wrote:
PHP5 has weak polymorphism but not true polymorphism in the sense that it
is used in OOP.

So how would you define polymorphism? And what exactly are the
differences between 'weak' and 'true' polymorphism?

If i look at
http://en.wikipedia.org/wiki/Polymor...ter_science%29

"The concept of polymorphism applies to data types in addition to
functions. A function that can evaluate to and be applied to values of
different types is known as a polymorphic function. A data type that
contains elements of different types is known as a polymorphic data
type."

function __construct($on e, $two="two", $three="three") {

I cannot instantiate this object as new Foo($one, $three) since there is
no typing on the arguments and, therefore, no signature for a 'one,
three' contructor.

Imho that's the same as saying: I'm standing with my back against a wall,
and now i'm wondering why i can't step backwards anymore...

Define your constructor as __constructor($ args) and handle with
func_num_args and func_get_args(s ) any number of parameters...

(I do agree that the language/compiler can, probably should, make this
easier... But that's a different discussion.)


The constructor overloading example isn't really polymorphism. It's just
function overloading.


Absolutely correct. Polymorphism means "same interface, different
implememtation" . You do not need overloading to make polymorphism work.

--
Tony Marston

http://www.tonymarston.net
http://www.radicore.org
Polymorphism is the ability to operate on objects of derived classes,
without knowing what the derived classes are (or potentially even the
existence of the derived classes).

For instance - let's say we have class "mammal" with function "eats". Now
all mammals eat something, but what the class of mammals eats is not
defines.

So, derive from mammal the class "ape" and have eats return "bananas".
Also derive the class "horse" and have eats return "oats".

Now - when you create an object of the class "ape" you can pass it to a
function which takes a "mammal". And you can print out what *this* mammal
eats. The same with a horse.

Polymorphism is closely tied to inheritance. But while inheritance allows
the programmer to take advantage of the commonalities between classes,
polymorphism allows the program to take advantage of the differences.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 25 '06 #30

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

Similar topics

0
1665
by: Benjamin C. Pierce | last post by:
The Twelth International Workshop on Foundations of Object-Oriented Languges (FOOL 12) Saturday 15 January 2005 Long Beach, California, USA Following POPL 05 The search for sound principles for object-oriented languages has given rise to much work on the theory of programming languages during the past two decades, leading to a better understanding of the key
5
2930
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented programming?
34
7122
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
65
4304
by: Roger Smythe | last post by:
A means for the progressive decomposition a problem space into increasingly simpler component parts such that these component parts represent higher levels of conceptual abstraction, and are completely independent of each other except for their well-defined interfaces. This was an attempt to explain the gist of OOP to programmers accustomed to the structured programming paradigm. I tried to explain OOP in terms of ideals that can be...
14
2942
by: Rookie | last post by:
Is C an object oriented programming language?
100
5321
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
8
1805
by: cppig1995 | last post by:
C++ is not only object-oriented language. I think the description of this group have to be changed. :-) Have fun! ---- cppig1995 (cppig1995@gmail.com) Daejeon Metropolitan City, Republic of Korea 11 years old in Western age.
47
5973
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web site at http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is very instructive. I know that I could do much of this stuff with e.g. C++, but the intellectual challenge of implementing these concepts with pure ANSI C is relevant to...
46
3042
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented language. When I read things like "... C++ is an evolution of C ..." or "... C is a subset of C++ ..." I tend to believe that I will have to learn C+ + sooner or later. It sounds like C++ is the future and C is the past (and will be no longer...
3
1551
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is an instance of a class. that i know, i also know how to program with classes etc. i am just confused about the term object-oriented.
0
9657
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10822
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10532
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.