Jump to content

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


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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...