PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 3rd, 2008, 11:25 PM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
Class public variable giving

PHP Code:
class Phrases {
    function 
phrase() {
        
$title = array(
            
'network' => 'Our Worthy Network Infastructure',
            
'support' => 'Reliable & Professional Support Services',
            
'about' => 'Our Mission Statement'
        
);
        foreach (
$title as $key => $value) {
            public 
$key $value;
        }
    }
}
$info = new Phrases(); 

Trying to execute this simple function to make sure my syntax is correct before pulling phrases from mysql, but keep getting a unexpected T_VARIABLE syntax error on the execution of the public setting.

Last edited by PiMPaRSeBiSh : May 4th, 2008 at 12:26 AM. Reason: removed quotes from $value

Reply With Quote
  #2  
Old May 4th, 2008, 12:01 AM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
My experience in classes is limited, however, I notice you do not have a constructor. And in the foreach, do you want the var $value in quotes?

Reply With Quote
  #3  
Old May 4th, 2008, 12:25 AM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
No, don't want the var in quotes, just forgot to remove them before posting. I had tried several variations before posting here, one of which wrapped the var in quotes. As for the constructor, I'm not looking to link any functions, only to call the variable via $info->phrase['network'].

Reply With Quote
  #4  
Old May 4th, 2008, 12:28 AM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
Quote:
Originally Posted by PiMPaRSeBiSh
No, don't want the var in quotes, just forgot to remove them before posting. I had tried several variations before posting here, one of which wrapped the var in quotes. As for the constructor, I'm not looking to link any functions, only to call the variable via $info->phrase['network'].


Are you still getting the error message without the quotes around the variable? If so, does your message say which line is causing the error to trigger?

Also, it seems you could get by without the class and use the function, which may be more efficient.

Reply With Quote
  #5  
Old May 4th, 2008, 12:32 AM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
Yes, it still gives the error without the quotes. This is the first way I tried. The line of error is 'public $key = $value;'. The class will bcome much more advanced, pulling info from mysql and the such. I just want to make sure it works before adding the more advanced modules.

Reply With Quote
  #6  
Old May 4th, 2008, 12:38 AM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
Quote:
Originally Posted by PiMPaRSeBiSh
Yes, it still gives the error without the quotes. This is the first way I tried. The line of error is 'public $key = $value;'. The class will bcome much more advanced, pulling info from mysql and the such. I just want to make sure it works before adding the more advanced modules.


Maybe the problem lies in your declaring the var public in the foreach. The var should be declared outside the function and the foreach (public $value; ) and the foreach should be left to process whatever you want to do if the conditions of the foreach are met.

Reply With Quote
  #7  
Old May 4th, 2008, 02:03 AM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
I got it to work with var, instead of public. thanks though.

Reply With Quote
  #8  
Old May 4th, 2008, 09:40 AM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
Quote:
Originally Posted by PiMPaRSeBiSh
I got it to work with var, instead of public. thanks though.


Good deal. However, you will need to declare variables within the class before you get into functions and such, or you may start getting more errors, especially as your class begins to grow and extend. Hope this is helpful.

Reply With Quote
  #9  
Old May 4th, 2008, 11:34 PM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
This is understood. Only problem I am having now is a nesting a classes. For example, having the main class and a couple classes inside (each having several of their own functions), one for db handling, one for template handling, and another for phrase handling. $class->subclass->function() Is this possible?

Reply With Quote
  #10  
Old May 4th, 2008, 11:37 PM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
Quote:
Originally Posted by PiMPaRSeBiSh
This is understood. Only problem I am having now is a nesting a classes. For example, having the main class and a couple classes inside (each having several of their own functions), one for db handling, one for template handling, and another for phrase handling. $class->subclass->function() Is this possible?


Classes don't nest. A subclass is a child of a parent class. It is a separate class but inherits from the parent.

Reply With Quote
  #11  
Old May 5th, 2008, 02:06 AM
PiMPaRSeBiSh PiMPaRSeBiSh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 72 PiMPaRSeBiSh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 sec
Reputation Power: 0
So how would I implement this? I'm looking to do something like this:
PHP Code:
class site {
    var 
$phrases $this->db->query("SELECT name, value FROM phrases WHERE group IN ('global', " $phrasegroups ")");
    class 
cache {
        function 
phrases() {
            
//code
        
}
        function 
templates() {
            
//code
        
}
    }
    class 
db {
        function 
connect() {
            
//code
        
}
        function 
query() {
            
//code
        
}
    }


So I can call:
$site->cache->phrases to cache phrases
$site->cache->templates to cache templates
$site->db->connect to connect to the db
$site->db->query to inject sql
and of course $site->phrases will contain the array of phrases from the db.

Reply With Quote
  #12  
Old May 5th, 2008, 08:39 AM
not666 not666 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: CDA, ID
Posts: 47 not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level)not666 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 46 m 35 sec
Reputation Power: 7
Quote:
Originally Posted by PiMPaRSeBiSh
So how would I implement this? I'm looking to do something like this:
PHP Code:
class site {
    var 
$phrases $this->db->query("SELECT name, value FROM phrases WHERE group IN ('global', " $phrasegroups ")");
    class 
cache {
        function 
phrases() {
            
//code
        
}
        function 
templates() {
            
//code
        
}
    }
    class 
db {
        function 
connect() {
            
//code
        
}
        function 
query() {
            
//code
        
}
    }


So I can call:
$site->cache->phrases to cache phrases
$site->cache->templates to cache templates
$site->db->connect to connect to the db
$site->db->query to inject sql
and of course $site->phrases will contain the array of phrases from the db.


The db connect class would be a stand alone class; not a parent or a child to the other two classes. I am not sure how these other classes are going to function in your code, but by first impressions, I would say the cache class would also be a stand alone class. My knowledge of classes is limited as I, too, am a novice at implementing them, so I am not the one to give you in-depth instruction on this matter and without knowing exactly what you are doing, I can't draw up examples. However, these are basic principles I am relating. You might want to do a little more study on class implementation. Sorry, I can't be more helpful on this matter. Maybe in another month or two - if you want to wait that long!!!

Reply With Quote
  #13  
Old May 5th, 2008, 09:07 AM
jcarouth's Avatar
jcarouth jcarouth is offline
Never give up
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2005
Location: College Station, TX, USA
Posts: 2,606 jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)jcarouth User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 1251 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 5 h 5 m 47 sec
Reputation Power: 693
Send a message via AIM to jcarouth Send a message via MSN to jcarouth Send a message via Yahoo to jcarouth Send a message via Skype to jcarouth
Facebook Orkut
please note that using var in php5 is only supported for compatibility reasons and should be considered deprecated behavior.

while it is possible, i don't know that you should be dynamically assigning members to a class. instead if you want this type of flexibility you should store the dynamic elements in an array member of the class. for example
PHP Code:
class foo
{
   public 
$my_dynamic_member;

   public function 
foo()
   {
      
$this->my_dynamic_member = array();
   }

   public function 
phrase()
   {
       
$title = array(
           
'network' => 'Our Worthy Network Infastructure',
           
'support' => 'Reliable & Professional Support Services',
           
'about' => 'Our Mission Statement'
       
);
       
      
$this->my_dynamic_member array_merge($this->my_dynamic_member$titles);
    }

regarding your other question, classes cannot be nested as you have attempted and seen. rather you *could* do something like this
PHP Code:
class DB
{
   public function 
__construct()
   {
      
//do something
   
}
}

class 
Template
{
   ...
}

class 
Site
{
   public 
$DB;
   public 
$Template;

   public function 
__construct()
   {
      
$this->DB = new DB();
      
$this->Template = new Template();
   }

which would allow access as you specified in your requirements. hope this helps.
__________________
Jeff Carouth's Homepage
Accelerate Studio

have i been exceptionally helpful today? feeling generous?

Reply With Quote
  #14