[PHP] A stupid question about classes
Hello everyone, Just a basic question. I have my class starting like this: Class OireMail { // these are required public $smtp_server=; public $domain=; public $from=; public $login=; public $pass=; And then go the function themselves. I was told that it's better to put the initial variables in the __construct() function. What are the advantages of doing that and if I need to do it, how would I call the class from another file then? Thanks! -- With best regards from Ukraine, Andre Http://oire.org/ - The Fantasy blogs of Oire Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: http://twitter.com/m_elensule -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A stupid question about classes
On 05/01/2010 10:23 PM, Andre Polykanine wrote: Hello everyone, Just a basic question. I have my class starting like this: Class OireMail { // these are required public $smtp_server=; public $domain=; public $from=; public $login=; public $pass=; And then go the function themselves. I was told that it's better to put the initial variables in the __construct() function. What are the advantages of doing that and if I need to do it, how would I call the class from another file then? Thanks! The advantages of initializing the variables in __construct() is that whenever an object of the class is created, the variables have the values you expect. If you don't put them in __construct(), you will have to create another method which will have to be called after you have created the object using the new operator. Ultimately its the same thing, __construct() is called automatically, only that's the difference. -- Nilesh Govindarajan Site Server Administrator www.itech7.com मेरा भारत महान ! मम भारत: महत्तम भवतु ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] A stupid question about classes
Hello Nilesh, So could you illustrate a bit the __construct() function, please? Should I pass those variables as parameters of that function? And what if I need to change their values?) Thanks! -- With best regards from Ukraine, Andre Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: m_elensule - Original message - From: Nilesh Govindarajan li...@itech7.com To: php-general@lists.php.net php-general@lists.php.net Date: Saturday, May 1, 2010, 8:16:37 PM Subject: [PHP] A stupid question about classes On 05/01/2010 10:23 PM, Andre Polykanine wrote: Hello everyone, Just a basic question. I have my class starting like this: Class OireMail { // these are required public $smtp_server=; public $domain=; public $from=; public $login=; public $pass=; And then go the function themselves. I was told that it's better to put the initial variables in the __construct() function. What are the advantages of doing that and if I need to do it, how would I call the class from another file then? Thanks! The advantages of initializing the variables in __construct() is that whenever an object of the class is created, the variables have the values you expect. If you don't put them in __construct(), you will have to create another method which will have to be called after you have created the object using the new operator. Ultimately its the same thing, __construct() is called automatically, only that's the difference. -- Nilesh Govindarajan Site Server Administrator www.itech7.com ! ?? : ?? ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] A stupid question about classes
On 1 May 2010 20:38, Andre Polykanine an...@oire.org wrote: Hello Nilesh, So could you illustrate a bit the __construct() function, please? Should I pass those variables as parameters of that function? And what if I need to change their values?) Thanks! ?php // The generic class which can act standalone. Class OireMail { // these are required public $smtp_server=; public $domain=; public $from=; public $login=; public $pass=; public function __construct($smtp_server = '', $domain = '', $from = '', $login = '', $pass = '') { $this-stmp_server = $smtp_server; $this-domain = $domain; $this-from = $from; $this-login = $login; $this-pass = $pass; } } // Let's create a generic email class and supply all the params. $Mail = new OireMail('Server', 'Domain', 'f...@domain.com', 'login', 'Passw0rd'); // A more specialised version of the class with all the required params pre set. Class SpecialisedOireMail extends OireMail { public function __construct() { parent::__construct('SpecServer', 'SpecDomain', 's...@specdomain.com', 'SpecLogin', 'SpecPassw0rd'); } } // Let's create a specialised version. Note - no need to supply params as the sub-class deals with that. $SpecMail = new SpecialisedOireMail(); var_dump($Mail, $SpecMail); ? outputs ... object(OireMail)#1 (6) { [smtp_server]= string(0) [domain]= string(6) Domain [from]= string(15) f...@domain.com [login]= string(5) login [pass]= string(8) Passw0rd [stmp_server]= string(6) Server } object(SpecialisedOireMail)#2 (6) { [smtp_server]= string(0) [domain]= string(10) SpecDomain [from]= string(19) s...@specdomain.com [login]= string(9) SpecLogin [pass]= string(12) SpecPassw0rd [stmp_server]= string(10) SpecServer } Hope that helps. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[4]: [PHP] A stupid question about classes
Hello Richard, thanks a lot! -- With best regards from Ukraine, Andre Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org Yahoo! messenger: andre.polykanine; ICQ: 191749952 Twitter: m_elensule - Original message - From: Richard Quadling rquadl...@googlemail.com To: Andre Polykanine an...@oire.org Date: Saturday, May 1, 2010, 10:49:49 PM Subject: [PHP] A stupid question about classes On 1 May 2010 20:38, Andre Polykanine an...@oire.org wrote: Hello Nilesh, So could you illustrate a bit the __construct() function, please? Should I pass those variables as parameters of that function? And what if I need to change their values?) Thanks! ?php // The generic class which can act standalone. Class OireMail { // these are required public $smtp_server=; public $domain=; public $from=; public $login=; public $pass=; public function __construct($smtp_server = '', $domain = '', $from = '', $login = '', $pass = '') { $this-stmp_server = $smtp_server; $this-domain = $domain; $this-from = $from; $this-login = $login; $this-pass = $pass; } } // Let's create a generic email class and supply all the params. $Mail = new OireMail('Server', 'Domain', 'f...@domain.com', 'login', 'Passw0rd'); // A more specialised version of the class with all the required params pre set. Class SpecialisedOireMail extends OireMail { public function __construct() { parent::__construct('SpecServer', 'SpecDomain', 's...@specdomain.com', 'SpecLogin', 'SpecPassw0rd'); } } // Let's create a specialised version. Note - no need to supply params as the sub-class deals with that. $SpecMail = new SpecialisedOireMail(); var_dump($Mail, $SpecMail); ? outputs ... object(OireMail)#1 (6) { [smtp_server]= string(0) [domain]= string(6) Domain [from]= string(15) f...@domain.com [login]= string(5) login [pass]= string(8) Passw0rd [stmp_server]= string(6) Server } object(SpecialisedOireMail)#2 (6) { [smtp_server]= string(0) [domain]= string(10) SpecDomain [from]= string(19) s...@specdomain.com [login]= string(9) SpecLogin [pass]= string(12) SpecPassw0rd [stmp_server]= string(10) SpecServer } Hope that helps. -- - Richard Quadling Standing on the shoulders of some very clever giants! EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php