Jump to content

Am I setting this classes up right at the top of my script?


wilsonf1
 Share

Recommended Posts

I've kind taught myself PHP whilst looking online for examples. I use classes, I just wonder if I'm setting up my classes the right way. Do you have to explicity give a class access to another class in the way I've done below?: $db = new dbConnectionMSSQL();$systemLog = new systemLog();$clubTickets = new clubTickets();$Customers = new Customers();$Events = new Events();$TicketOrders = new TicketOrders();$PlacesOfInterest = new PlacesOfInterest();$Tonight = new Tonight(); $clubTickets->db = $db;$clubTickets->Customers = $Customers;$clubTickets->Events = $Events;$clubTickets->PlacesOfInterest = $PlacesOfInterest;$clubTickets->TicketOrders = $TicketOrders;$clubTickets->Tonight = $Tonight; That's what I've been doing up until now. Is there a more efficient way to do it? Every time I create a new class, I need to copy the bold coce above for that class. Or is that just life in PHP?

Edited by wilsonf1
Link to comment
Share on other sites

you could pass an array in construtor which will initialize its value by looping it. something like

public function __construct($param=array('db'=>'dbcredential','customer'=>'some name','event'=>'spme event')){    foreach($param as $key=>$value){   $this->$key=$value;}}

if you want that no other property be set rather than the property you initialized at first you ,can use isset() in loop before assigning the value

Link to comment
Share on other sites

You could probably replace several of those with static classes, which would be global and you wouldn't need to set them as properties for another object.
Do you have an example of them being "global" ? I think what you are suggesting is right - I just haven't done it before so could do with an example of setting them up, and usage. Cheers :)
Link to comment
Share on other sites

There's a description about static members here: http://www.php.net/m...oop5.static.php You don't do anything to make it global, if you define a class with a static method or property then you can access that at any place using the name of the class and the name of the member. Just in the way of terminology, the class itself isn't static, the members are. I usually refer to a class that only contains static members as a static class.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...