473,480 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

-> or :: ?

a
hi all,

I'm a bit confused with this and I hope you can help me:

I have a "Mother" class and a "child" that extends Mother.
The child's constructor should call Mother's constructor.

Now the question is:
How should child's constructor call mother's constructor?
a) Mother::Mother();
b) $this->Mother();

I guess the second way is the right one, but I'm confused because:
1) both methods work;
2) both methods are in the manual;

can you help me to understand this (so I can sleep again ;-) )?

TIA
FR
----
The code:

<?php

class Mother
{
var $v;
function Mother()
{
echo "I'm the Mother<br>\n";
$this->v=1;
}
}

class ChildA extends Mother
{
function ChildA()
{
Mother::Mother(); // <--- is this correct? why?
echo "I'm the childA<br>\n";
}
}

class ChildB extends Mother
{
function ChildB()
{
$this->Mother(); // <--- is this correct? why?
echo "I'm the childB<br>\n";
}
}

$a=new childA();
$b=new childB();

echo "\$a->v: $a->v<br>\n";
echo "\$b->v: $b->v<br>\n";

?>
Jul 17 '05 #1
3 1661
a@b.c wrote:
hi all,

I'm a bit confused with this and I hope you can help me:

I have a "Mother" class and a "child" that extends Mother.
The child's constructor should call Mother's constructor.

Now the question is:
How should child's constructor call mother's constructor?
a) Mother::Mother();
b) $this->Mother();

I guess the second way is the right one, but I'm confused because:
1) both methods work;
2) both methods are in the manual;

can you help me to understand this (so I can sleep again ;-) )?


You should understand OOP a bit more.

Using :: is invoking a class's method as static, i.e. directly from the
class; essentially, there is no difference between that and classic
user-defined functions, except that you can have several functions of the
same name and just call them using a different "prefix" (class name) in
different situations.

The -> notation is used when you call a method, ie. a function attached to
an actual object.

In code, you use static method like this:

YourClass::yourFunction();

However, to call a method you need to have an object instantiated first:

$object = new YourClass();
$object->yourFunction();

The advantage of this is that the method is "attached" to the object, and
already "knows" everything about it. You can do the same by a static method
(or a classic function), but you would have to pass the object as a
parameter first.

This method thing might not seem like much, but this is just a facet of
OOP -- there is inheritance, with subclasses extending other classes, and
polymorphism, with same methods doing different things depending on their
object's class (so accelerate() does one for your generic Car class, and
something completely different for its Ferrari subclass) and so on.

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Jul 17 '05 #2
Berislav Lopac wrote:
You should understand OOP a bit more.

Using :: is invoking a class's method as static, i.e. directly from the
class; essentially, there is no difference between that and classic
user-defined functions, except that you can have several functions of the
same name and just call them using a different "prefix" (class name) in
different situations.

The -> notation is used when you call a method, ie. a function attached to
an actual object.

In code, you use static method like this:

YourClass::yourFunction();

However, to call a method you need to have an object instantiated first:

$object = new YourClass();
$object->yourFunction();


Yeah, but calling the parent class constructor from a child's constructor
either way accomplishes the same - i.e. not a static function call, but a
method call that may change the values of the properties of the object
being instantiated. That's how I understood the OP's question. There is one
more way to get the same result in PHP4:

parent::ParentClass();

and, in fact, in PHP5, the "correct" way to call parent class constructor
is:

parent::__construct();
Jul 17 '05 #3

<a@b.c> wrote in message news:40***********************@news.free.fr...
hi all,

I'm a bit confused with this and I hope you can help me:

I have a "Mother" class and a "child" that extends Mother.
The child's constructor should call Mother's constructor.

Now the question is:
How should child's constructor call mother's constructor?
a) Mother::Mother();
b) $this->Mother();

I guess the second way is the right one, but I'm confused because:
1) both methods work;
2) both methods are in the manual;


I would say that invocation through :: is the preferred syntax. Basically a
way to acknowledge the difference behind inherited the default behavior and
calling a method. As you noted, the two works the same. The function invoke
statically would still get the $this variable.
Jul 17 '05 #4

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

Similar topics

1
6793
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
3182
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
10529
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
0
2034
by: Arne Schirmacher | last post by:
I want to display a MySQL database field that can contain HTML markup. If I use <esql:get-string> then I get all of the database field, but all tags are escaped which is not what I want. If I use...
34
10963
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
11
13649
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
2
2428
by: bissatch | last post by:
Hi, I am currently writing a simple PHP program that uses an XML file to output rows for a 'Whats New' page. Once written, I will only require updating the XML file and any pages that use the...
0
1067
by: vdex42 | last post by:
Apologies if this has been asked before, but I haven't been able to find the answer to this yet: My problem is that .NET will not allow me to insert escaped '>' characters (i.e. &gt;) within the...
2
2020
by: santaji | last post by:
I am getting xml string in request attribute in following format &lt;files&gt; &lt;file&gt; &lt;filename&gt;somefile.ext&lt;/filename&gt; &lt;/file&gt; &lt;files&gt; the above string I want to convert to tags. expected...
1
5458
by: VaidehiPawar | last post by:
I am a beginner level in xml..my output page does not convert &gt &lt it shows something like this " &lt;b&gt;Location.&lt;/b&gt;&lt;br /&gt; &lt;UL&gt;&lt;LI&gt;Park Central New York " can anyone help? here is my code ...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
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...
0
7037
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,...
0
6895
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...
0
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
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...
0
2992
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...
0
1296
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 ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.