12 Replies - 1579 Views - Last Post: 14 June 2010 - 07:16 AM Rate Topic: -----

#1 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Php-Calling multiple classes from with in a class

Posted 13 June 2010 - 06:48 PM

Hey all,
i have these pages right:
Classes.php
<?php
class attack
{
function bow()
{
include "/attack_bow.php";
$bow = new bow;
$i = $bow->calc_distance();
return $i;
}
}
?>


attack_bow.php
<?php
class bow
{
function calc_distance()
{
function stuff goes here
return $variable
}
function him()
{
function stuff goes here;
return $variable;
}
}
?>



test.php
<?php
include "classes.php";
$h = new attack;
$h->bow();
?>



Now my question is, can i call the him function with like
$h->bow()->$bow->him();


or will i have to add variables.if statements and all that

Thanks Aaron1178

Is This A Good Question/Topic? 0
  • +

Replies To: Php-Calling multiple classes from with in a class

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2984
  • Posts: 10,316
  • Joined: 08-August 08

Re: Php-Calling multiple classes from with in a class

Posted 13 June 2010 - 07:29 PM

Wouldn't it be better to let the controller decide what methods of what object to call?
Was This Post Helpful? 0
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 13 June 2010 - 10:54 PM

well no, because i want to be able to choose witch funtions to control lol,
i want to choose for example
$h->bow()->calc_distance();
$h->bow()->him();


and like that, but im unable to know how :D

Please help, i have tryed heaps of ways, but Unsuccessful.

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

#4 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 12:45 AM

If you return the $bow instance from your attack::bow() method, you could.

class attack {
    function bow() {
        $bow = new bow;
        return $bow;
    }
}

/** bow's class definition [...] */

$h = new attack;
$h->bow()->him();



Mark.
Was This Post Helpful? 3
  • +
  • -

#5 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 02:45 AM

Ah ok Cheers,
But what if i wanted to add perameters to bow() function, to parse on to him() function.
Like if bow() was to construct my thing

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

#6 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 02:48 AM

Then pass them as you would normally.

class attack {
    function bow($param1, $param2) {
        $bow = new bow($param1, $param2);
        return $bow;
    }
}

/** bow's class definition [...] */

$h = new attack;
$h->bow()->him();



I must say, though, this design is somewhat lacking.
Was This Post Helpful? 0
  • +
  • -

#7 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 03:55 AM

ah ok, thanks,
What do you mean, lacking,
like in code or what?:)
because if you mean with code, this is a beta of it and im not displaying the full code, e,g: im not showing the function guts lol. But im only new to OOP, i cant expect to be the best(Not yet anyway).

My idea when its finished, its going to quit sophisticated.

As i said, im only new to OOP and there are no "Good" tutorials on this matter.

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

#8 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 04:19 AM

Well, for one, I don't understand why the 'attack' class has a 'bow' function that returns an object of type 'bow'. Seems a little convoluted. Plus, what happens when you add more attacks? You have to keep adding code. In this instance, you might want to check out what we call a Factory Method; it decides for you which object should be instantiated, based on some information you give it.

class AttackFactory {
    public static function create($type) {
        if (!class_exists($type)
             trigger_error("Attack class $type does not exist", E_USER_ERROR);
        return new $type;
    }
}

/** rest of code */

$bow = AttackFactory::create('bow');



However, I don't really know what's going on with your code, so I'm implying a few things.

Mark.

P.S. There are lots of good books on OOP. OOP is a concept, a paradigm, and, as such, is not tied to a specific language or syntax - the skills are transferable.
Was This Post Helpful? 1
  • +
  • -

#9 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 05:18 AM

Ahk cool :)
but i was goin to have it like this
<?php
class attack
{
function bow()
{
include "/attack_bow.php";
$bow = new bow;;
return $bow;
}
function spell()
{
include "/attack_spell.php";
$spell = new spell;
return $spell;
}
}
?>


Something like that, im totally new to OOP, but im and Php-Master(So i reckon :))
So im just trying to make the code as easy to read by me lol

I like the factory method, but im trying to use my own style of coding :)

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

#10 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 05:35 AM

sorry internet timed out

This post has been edited by aaron1178: 14 June 2010 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#11 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 05:43 AM

\sorry, my internet is going real slow and it timed out

This post has been edited by aaron1178: 14 June 2010 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#12 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 06:08 AM

Having your own style is cool. Ignoring tried-and-tested methods of programming (design patterns), however, is counter-productive. But if you want to reinvent the wheel...
Was This Post Helpful? 0
  • +
  • -

#13 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Php-Calling multiple classes from with in a class

Posted 14 June 2010 - 07:16 AM

ahk, i didnt ignore it, i just dont understand it :)
only write what i understand lol, dont get me wrong, i know its good code, but to write code you must first understand code,
if you get my point of view.

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1