473,322 Members | 1,287 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,322 software developers and data experts.

Building on classes -- inheritance help needed

Hi There,
I'm trying to understand inheriting properties object and I'm not
fully understanding the ways to accomplish this.

For example, I have a PHP array that is generate in "class a". I
would like to use this array with its values in "class b". When I
create a new instance of "class b" I get the array but none of the
values are present. Make sense? An explanation on how to accomplish
this would be great. A simplified example is below. Any help /
guidance would be greatly welcome. -- CH

class a {
public $one;

public function someMethod() {
$one = array("1");
}
}

class b extends class a {
public $one;
public $two

function someOtherMethod() {
print_r($this->one);
}
}

$display = new a;
$displayb = new b($a);
$displayb->someOtherMethod();

//outputs
Array ( [0] =)

Mar 21 '07 #1
4 1158
mountain.dog wrote:
$display = new a;
$displayb = new b($a);
$displayb->someOtherMethod();
You're misunderstanding what inheritance is; so the second line of the
above excerpt doesn't do what you think it does. Replace those three
lines with:

$displayb = new b();
$displayb->someMethod();
$displayb->someOtherMethod();

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 21 '07 #2
mo**********@gmail.com wrote:
Hi There,
I'm trying to understand inheriting properties object and I'm not
fully understanding the ways to accomplish this.

For example, I have a PHP array that is generate in "class a". I
would like to use this array with its values in "class b". When I
create a new instance of "class b" I get the array but none of the
values are present. Make sense? An explanation on how to accomplish
this would be great. A simplified example is below. Any help /
guidance would be greatly welcome. -- CH

class a {
public $one;

public function someMethod() {
$one = array("1");
}
}

class b extends class a {
public $one;
public $two

function someOtherMethod() {
print_r($this->one);
}
}

$display = new a;
$displayb = new b($a);
$displayb->someOtherMethod();

//outputs
Array ( [0] =)
Two problems.

First of all, $one in class 'b' overrides $one in class 'a'. So you end
up with two variables named $one - one in 'a' and one in 'b'.

Secondly, you don't need

$display = new a;

When you create an instance if 'b' you automatically get an instance of
'a'. 'a' is part of 'b's implementation.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 21 '07 #3
Thank you Toby. Your brief example helped clarify a lot. I appreciate
your help. One question - what's the difference between these two
instances?
$displayb = new b();
versus
$displayb = new b;
thanks!

On Mar 21, 11:27 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
mountain.dog wrote:
$display = new a;
$displayb = new b($a);
$displayb->someOtherMethod();

You're misunderstanding what inheritance is; so the second line of the
above excerpt doesn't do what you think it does. Replace those three
lines with:

$displayb = new b();
$displayb->someMethod();
$displayb->someOtherMethod();

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Mar 21 '07 #4
mo**********@gmail.com wrote:
One question - what's the difference between these two
instances?
$displayb = new b();
versus
$displayb = new b;
In this case, no difference.

You can however, write a class with a constructor function which takes one
or more arguments, in which case the brackets are required:

$displayb = new b('something', 123, 456);

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 21 '07 #5

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

Similar topics

18
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming...
4
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
4
by: =?Utf-8?B?Uml6d2Fu?= | last post by:
In the system I am working on, there are 3 credit cards: Visa, MasterCard and AmericanExpress. On the database side I have one table: credit_card CREATE TABLE credit_card ( credit_card_id int...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.