child class question ?? how can i do that?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

child class question ?? how can i do that?

Post by mudkicker »

hi people.

i'm still developing my basic directory class but i wanted to do it in seperate files and with sub-classes for more optimized and clean coding.

what i want to do is ->

seperate files for sepereate classes like

error_class.php
---------------------

Code: Select all

<?php
class Error extends Directory {
// codes
}
?>
template_class.php
---------------------

Code: Select all

<?php
class Template extends Directory {
// codes
}
?>
etc.

like this and include them in index.php

Code: Select all

<?php

@require("dizin_class.php");
@require("template_class.php");
$template = new Template("Mudkicker Online");
$dizin = new Directory;
$template->Footer();

?>
is this possible?

and can i use variable between subclasses?

thanks for your help.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yep.

But you cant share the variables, unless you pass a copy of the object to the other class.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

kettle_drum wrote:Yep.

But you cant share the variables, unless you pass a copy of the object to the other class.
what do you mean with that? would you explain it with an example?
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

Hello,

I didn't tried it, but i think (theoretical view of this problem).

When you have two sub-classes and both have inhirited the basic class, then you have the in both the same vars and function.

Now you create 2 objects (from each sub-class one), this object are independent, and haven't a connection to each other. they only have the same kind of vars and funktions.

In PHP you dont have pointers (i think so). In C++ you can tell an object a pointer of another object, and then the first knows the latter.

but in php its not possible. when one object knows the other it own it (it is a var of the class)

greez Pozor
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Yea what pozor says it right - just think of it as two different copies of the class with all the blank (variables) filled in.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

aargh...
i think i'm going to be crazy.. my brain stopped :(
can anybody explain this with examples please :( :( :(
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Ok here we go:

Code: Select all

class A {
   var $myVar1;
   var $myVar2;

   function A(){ //class constructor - will be run when you initiate an instance of the classs
      $this->myVar1 = "hello";
      $this->myVar2 = " world";
   }

   function print(){
      echo $this->myVar1.$this->myVar2;
   }

}

class B extends A {
   
   function B($var1, $var2){
      $this->myVar1 = $var1;
      $this->myVar2 = $var2;
   }
}
Those are the classes, now we call them.

Code: Select all

$myClass = new A();
$myClass->print();
"hello world" is printed to thes screen, as we call class A, it runs the constructor and places "hello" and " world" in the classes vars.

Code: Select all

$myClass = new B("Hello", " Johnson");
$myClass->print();
This time "Hello Johnson" is printed, as in class B you have to pass some initial vars to the class constructor which get put into the class vars.

Code: Select all

$myClass = new B("Hello", " Johnson");
$myClass->print();
$myClass2 = new B("Hello", " Billy");
$myClass2->print();
Now we call two instances of the B class and give them different parameters, they then print what they are given - so "Hello JohnsonHello Billy" is printed since we didnt put a break betweet the two calls.

The two classes we initalise DONT share the same actual variables, but both have access to the variables that were declaired in the parent class or the class itself.

Umm...A class is just a template that you can call instances of. When you do (by doing $blah = new Class();) you provide space in the computers memory for any variables - called properties - in the class and parent classes. By extending from another class you inherit the parents class methods and properties, which is why you can use the parents methods and properties.

Hope this helped abit.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Sure it helped thanks a lot!!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Hey just a question - in php can you have mulitple constructors in the same class, just taking different arguments? I know in java you can and its very useful so you can have default values or assign your own by passing them as arguments and it automatically picks the appropriate constructor.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Umm as far as i know you cant, although php 5 might allow you to. If it doesnt then you can solve it in two ways.

Code: Select all

function ClassConstructor($var, $var2, $var3 = 0, $var4 = 0){
  ...
}
like that and so if you dont enter the last two varsthey are set to a default value - in this case 0.

Then you can pass an array of variables to the constructor and add a little code to have a look through the array to see what was passed.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

oh yea i forgot about passing variables that way! I guess thats just as good....
Post Reply