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