Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

OOP - Share Properties With Parent and Extended Classes
Welcome, Guest. Please login or register.
November 20, 2008, 10:06:46 AM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Serverside Technology  |  PHP  |  OOP - Share Properties With Parent and Extended Classes « previous next »
Pages: [1] 2
Author Topic: OOP - Share Properties With Parent and Extended Classes  (Read 853 times)
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« on: March 22, 2008, 09:13:36 PM »

I have a class named A which is extended by B and C

A has property AA which I want to be able to access in B and C

also B has property BB which I want to access in A and C.

How would I go about doing this without having to make a global? Thanks for the help!

Code
Language: php (GeSHi-highlighted)
<?php
 
class A
{
   public $AA = 'aaa';
   
   function print_bb()
   {
       // echo $BB....????
   }
}
 
class B extends A
{
   public $BB = 'bbb';
   
   function print_aa()
   {
       // echo $AA....????
   }
}
 
class C extends A
{
 
   function print_aa()
   {
       // echo $AA....????
   }
   
   function print_bb()
   {
       // echo $BB....????
   }
}
 
?>
Logged

[x] Fight | www.crypticmauler.com
"You must be
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« Reply #1 on: March 24, 2008, 08:14:26 AM »

> A has property AA which I want to be able to access in B and C
That's piece of cake.

> also B has property BB which I want to access in A and C.
That does not make sense... B's properties and methods are only available in B or in child classes of B.

Code
Language: php (GeSHi-highlighted)
<?php
 
class A
{
public $AA = 'aaa';
 
function print_aa()
{
echo $this->AA;
}
}

class B extends A
{
public $BB = 'bbb';
 
function print_bb()
{
echo $this->BB;
}
}

class C extends A
{
public $CC = 'ccc';
 
function print_cc()
{
echo $this->CC;
}
}
 
$myA = new A();
$myB = new B();
$myC = new C();
 
echo '<h4>PRINT AA FROM CLASS A</h4>';
$myA->print_aa();
echo '<h4>PRINT AA FROM CLASS B</h4>';
$myB->print_aa();
echo '<h4>PRINT AA FROM CLASS C</h4>';
$myC->print_aa();
echo '<h4>PRINT BB FROM CLASS B</h4>';
$myB->print_bb();
echo '<h4>PRINT CC FROM CLASS C</h4>';
$myC->print_cc();
 
?>
Logged
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« Reply #2 on: March 24, 2008, 08:32:24 AM »

... or maybe you are looking for method overriding???

Code
Language: php (GeSHi-highlighted)
<?php
 
class A
{
public $AA = 'aaa';
 
function print_property()
{
echo $this->AA;
}
}

class B extends A
{
public $BB = 'bbb';
 
function print_property()
{
echo $this->BB;
}
}

class C extends A
{
public $CC = 'ccc';
 
function print_property()
{
echo $this->CC;
}
}
 
$instances = Array(new A(), new B(), new C());
foreach ($instances as $inst)
{
$inst->print_property();
}
 
?>
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #3 on: March 25, 2008, 11:46:34 AM »

Ok so its possible for C to access a prop in A and B to access a prop in A.

Then to have B access a prop in C, can I possibly clone the prop in A so then it will be a prop of A too and not just a child prop, which can then be accessed by B or any childs?


EDIT:
doh just realized you can't 'clone' a prop only an object. I suppose I can possible do this...

$this->parent_var = $this->child_var;

by making a prop in A that is set to one of the child's props I can then access it through any child correct?
« Last Edit: March 25, 2008, 11:51:35 AM by CrYpTiC_MauleR » Logged

[x] Fight | www.crypticmauler.com
"You must be
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« Reply #4 on: March 25, 2008, 03:04:52 PM »

That's not how OOP works.
Child classes only inherit methods and attributes of THEIR PARENTS.
If B is not a PARENT for C, an instance of C is NOT an instance of B and therefore does not have access to attributes or methods defined in B.

Maybe what you want is this:
   A
   B extends A
   C extends B
Then:
   An instance of B is an instance of A.
   An instance of C is an instance of B and an instance of A.

Let's take the animal analogy:

   class Animal
   class Bird extends Animal
   class Fish extends Animal

   $chicken instance of Bird
   $tuna instance of Fish

Now, you are asking for $tuna to access attributes defined in Bird.
That does not make any sense: $tuna is not a Bird.

What are your classes?  Give more details so we can fix your Object Model.
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #5 on: March 25, 2008, 04:23:44 PM »

I have a class named A which is extended by B and C
> "extended" : derived from ?
> So B and C both inherit from A alone ?

A has property AA which I want to be able to access in B and C
> this is standard, "piece of cake" as previously written

also B has property BB which I want to access in A and C.
> this is impossible, "that's not the way OOP works" as previously written

in your case, the answer is simple : your object model is flawed. If B has property BB and C and A need access to it, A and C have to inherit from A
As B and C already inherit from A, it means you should have either :
- a merge of A and B named "AB", and C inheriting from AB
- a common ancestor for both A and B, named "superA", having both properties AA and BB, and A,B,C derived from this common ancestor with A and B using only a subset of the ancestor's attributes (namely, AA and BB), while C would use AA if I understood well.

I'm no OO specialist at all, and I hate OOD/OOA/OOP, but I think I know the basics. Given multiple inheritance is really a pain in th eneck for some languages, and given circular derivation is forbidden, I guess the common ancestor solution is the way to go. Anyway, all objects derive from a common ancestor, TObject ;-)
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #6 on: March 26, 2008, 04:05:29 PM »

Thanks for clearing this all up guys. I am still learning OOP and this arose. So to be consistent with OOP I will just make a global variable in the child class thus not making a prop of it which can then be accessed by anything. I just wanted a way to access a valuemade by a class from any class, and my method would break my OOP objective so I suppose a global will be best option. If I am still going about this wrong please correct me. Thanks!
Logged

[x] Fight | www.crypticmauler.com
"You must be
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #7 on: March 26, 2008, 08:59:32 PM »

Quote
What they said
and creating a global breaks the OO also.

Can you be more specific about the purpose of your classes, esp. why CC should have access to a property in BB?

Logged

Rod
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #8 on: March 26, 2008, 10:32:10 PM »

I am making a collection of classes for part of a security suite, and class Log is getting the user's IP address. Class Log extends class Security and class Ban extends class Security. I want to be able to get the IP from Log to Ban or any other child class that may need access to it. Otherwise I am going to have to re-retrieve the IP.
Logged

[x] Fight | www.crypticmauler.com
"You must be
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« Reply #9 on: March 27, 2008, 11:10:48 AM »


That's exactly the info i was asking for in my last post.



There is a huge difference between a CLASS and an INSTANCE.
It's very important use those terms properly, otherwise we won't understand each other...

Classes can have methods and attributes.  They are called STATIC.
You don't need an instance to access the static methods and attributes.
Static methods are fine.
Static attributes are only fine when they are "constants" ("static final" in Java), otherwise they are equivalent to "globals".

The typical example is the Math class: "Math.pi" is a static constant, "Math.sin()" is a static method.

All the methods and attributes that are not static are methods and attributes of INSTANCES.



Having clarified that, you mean that an instance of Log is getting the user's IP and an instance of Ban wants to access it?  Is that correct?

How about something like this?

      $myLog = new Log();
      $myIP = $myLog->getIP();



Like VGR, I hate OOP's "inheritance".  On the other hand, i love everything else in OOP.
Famous article against inheritance: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html
Only use inheritance when there is absolutely no doubt there is a child/parent relationship.
If there is one small doubt, then use an attribute to store the relationship:

      Log "has a" Security
      Ban "has a" Security

This might require a little more code, but it's much better.
It might be a good idea to pass the related instance in the constructor:

      $mySecurity = new Security();
      $myLog = new Log($mySecurity);
      $myBan = new Ban($mySecurity);

And maybe also pass the Log instance to Ban's constructor:

      $myBan = new Ban($mySecurity, $myLog);



Could you explain what you need the "Security" class for?

From your questions and from the names of the classes, I would have guessed exactly the opposite relationships:

      Security "has a" Log
      Security "has a" Ban

As you can see, it's extremely difficult to know what your Object Model should be if we don't know what the classes are supposed to represent.
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #10 on: March 29, 2008, 04:37:22 PM »


How about something like this?

      Class Security {
             Public IP = '';
             
             Function getIP() {
                      return IP;
             }
      }

      Class Log extends Security {
           // Log properties, etc
     }

      Class Ban extends Security {
           // Ban properties, etc
     }

     // Both Log and Ban have IP and getIP, so can we do:
      $mySecurity = new Security();
      $mySecurity->IP = $SERVER["HTTP_REMOTE_ADDR"];  // get the IP, then

      $myLog = new Security($mySecurity());
      $myBan = new Security($mySecurity());

Now $myLog and $MyBan are both Security and contain the IP, don't they?

Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #11 on: March 30, 2008, 02:05:39 PM »

It's awesome how much time can be spoiled in OOP ;-))
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #12 on: March 30, 2008, 06:51:18 PM »

Yes, but it can also be elegant at times.  I don't like PHP's implementation, however.
Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #13 on: April 01, 2008, 12:50:57 AM »

assembler can be elegant, as are some COBOL or FORTRAN tricks (unnamed common block for instance ;-)

For High-level languages : Ada is elegant, as are Modula-2 and the various Pascal(s)

The others can not be.

PHP can not be elegant since it's mainly a mix of C and BASIC syntax and hasn't "IMPLICIT NONE" and 'STRONGTYPING ON" directives....
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #14 on: April 05, 2008, 11:08:16 PM »

Modula-2

Fond memories.  I still have a Modula-2 compiler and a data structures text.  It was very easy to do complex things in a simple, clean bit of code.
Logged

Rod
Pages: [1] 2
« previous next »
    Jump to: