Object Orient PHP Question

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
env
Forum Newbie
Posts: 8
Joined: Wed Sep 19, 2007 12:39 pm

Object Orient PHP Question

Post by env »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am designing a class that gets some information from a cluster of servers.
I have a class already that can obtain information from a single Server Object, but I want to be able to group some servers and call it a Cluster. Since PHP does not allow for overloading how can I implement this where the number of servers within a cluster can be any number instead of a fixed amount?

Code: Select all

//This creates a new server object
$server = new Server("$a_arg1","a_arg2","a_arg3");

//I want to create a cluster of servers
$cluster = new Cluster($server1, $server2, $serverN);
Essentially,
I suppose I am asking if I pass objects in as arguments to a class?

Thanks,
~Eric


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

While it's possible to pass any number of arguments to a method/function, I don't recommend it normally.

In this case, you could have a method in Cluster called addServer() which adds a server. 8O
env
Forum Newbie
Posts: 8
Joined: Wed Sep 19, 2007 12:39 pm

Post by env »

Ok, I see your point.
So maybe I don't need to specify the servers as parameters for a constructor, and could use an accessor method as you suggest.

So once I declare a new Cluster and add a Server to it, how do I access the attributes of that individual server?
I could use an array of Server objects with the Cluster is suppose.
Maybe some code might make more sense than what I'm trying to say...
This would be a Server class example:

Code: Select all

<?php
 //Server_class.php

    class Server{

        private $m_serverName = NULL;

        function __construct($a_serverName){
            $this->m_serverName = $a_serverName;
        } //end __construct

        function __destruct(){
            $this->m_serverName = NULL;
        } //end __destruct

        function getServerName(){
            return $this->m_serverName;
        } //end getServerName

    }; //end Server_class
?>
This would be a Cluster class example (using your approach):

Code: Select all

<?php
//Cluster_class.php
include("Server_class.php");

    class Cluster extends Server{

        private $m_clusterName = NULL;
        private $m_serverArray  = array(); //holds Server Objects

        function __construct($a_clusterName){
            $this->m_clusterName = $a_clusterName;
        } //end __construct

        function __destruct(){
            $this->m_clusterName = NULL;
            unset($this->m_serverArray);
        } //end __destruct

        function addServer($a_serverObject){
            array_push($this->m_serverArray, $a_serverObject);
        } //end addServer

        function getServerArray(){
            return $this->m_serverArray;
        } //end getServerArray 

    }; //end Cluster class
Do I even need to extend Server? Since I am just adding an Object to an array, couldn't I just use an array index and then the classes accessor functions?

Code: Select all

<?php
//some implementation...
include_once("Cluster_class.php");

    $myServer = new Server("SERVER1"); 
    //This should get me the server name
    echo "Server Name:\t".$myServer->getServerName();
    echo "\n";

    $myCluster = new Cluster("CLUSTER1");
    //Lets add SERVER1  to CLUSTER1
    $myCluster->addServer($myServer);

    //So now if we wanted to get the name of a server in our cluster, 
    //we would have to do something like this...
    $anArray  = $myCluster->getServerArray();

    //At this point, we know there is only one server object in the array. 
    ///Rather than going off on an array tangent, I
    //shall simply index the one object I know to be there.
    $sameName = $anArray[0]->getServerName();

    //This should give me the same output as the previous echo...
    echo "Server Name:\t".$sameName;
    echo "\n";

?>
This seems to be a bit messy as far as object oriented design goes. But I suppose its the route I must take...
If I were to extend the Server class, I might would want to do something like this (although probably not proper syntax)

Code: Select all

<?php
    $myServer = new Server("SERVER1");
    $myCluster = new Cluster("CLUSTER1", $myServer);

    $aServerInMyClustersName = $myCluster->myServer->getServerName();
?>
Thanks for the help.
I will use the array approach.

Lastly, can you recommend any sights that are tailored specifically for Object Oriented PHP? Most of what I find on searches are very basic in nature and mostly cover syntax.
Thanks!

~Eric
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your Cluster appears to be a Collection archetype, at this time. If you can say that a Cluster is a Server then it should extend Server.

As for stuff specifically about more advanced Object Oriented PHP, you're probably not going to find a lot. The reason why is OOP is more conceptual than concrete. Its methods and techniques aren't locked to any language.
Post Reply