Re: [PHP] Classes and access to outside variables
On 10/1/07, Merlin <[EMAIL PROTECTED]> wrote: > > Hello everybody, > > thank you for all your answers. I am sorry but I just realized that > there is an huge difference between PHP5 and PHP4 considering OOP. > Unfortunatelly I am still running PHP4.x and cant change this over > night. However I still need to implement this class which does work very > well, just needs handwritten parameters again. > > There is one file where the DB login data is stored and the new ajax > class I am using needs this data as well. Right now I am changin this in > both files, as I could not figure out how to access the variable from > inside the class. The code Martin has written seems only to work in PHP5 > as I do get errors. > > For now all I do want is to access an outside variable from inside a > class with PHP4 code. Thats all. I will look into classes more deaply in > the next days and in PHP5, but it would be very helpful if somebody > could help me now with getting that variable. > > Thank you for your help. > > Best regards, > > Merlin > Merlin, in php4 there are no restrictions on variable access from outside the class. you can access any member variable from an instance of the class directly, here is an example: class SomeClass { var $someVar = 'hello'; } $classInstance = new SomeClass(); echo $classInstance->someVar; /// prints hello i recommend you not do this however, since standard practice is to mark variables private and expose access to them through public methods. as you indicated php4 does not have ppp support. but, if you want a tip on how to write decent oo code in php4 here it is; drive all access to member variables through methods. when you later (if ever) port the code to php5, the member variables can be marked private and the member functions can be marked public. here is a revision of the above example to demonstrate: class SomeClass { var $someVar = 'hello'; /// getter method; return the value of the instance variable function getSomeVar() { return $this->someVar; } /// setter method, change the value of the instance variable function setSomeVar($someVar) { // some validation perhaps $this->someVar = $someVar; } } $someClassInstance = new SomeClass(); echo $someClassInstance->getSomeVar (); /// prints hello $someClassInstance->setSomeVar('wow'); echo $someClassInstance->getSomeVar(); /// prints wow that is good practice per php4 oop programming (imho). if it doesnt run you may have to weed out a typo or 2, i just wrote it right into the email. also, you dont have to pass data into the constructor only. typically, you only pass data into a constructor that a constructor may need to create an initial state for the object instance. if an object instance is valid w/o some data youre thinking about passing into the constructor, it may be better to just have a separate setter method. -nathan
Re: [PHP] Classes and access to outside variables
That's incorrectly syntactically. Won't run. Ok, let's write some code to show how this can be done: class SearchHelper extends AjaxACApplication { private $dbh; public function __construct() { $this->dbh = $GLOBALS['dbh']; } } Better yet, pass the database host in the construction: class SearchHelper extends AjaxACApplication { private $dbh; public function __construct($dbh) { $this->dbh = $dbh; } } $searchHelper = new SearchHelper($dbh); Even better, DON'T use globals: class DatabaseConnectionInfo { private var $host; ... public function getHost() { return $this->host(); } ... private static $defaultConnectionInfo; public static function getDefault() { if (self::$defaultConnectionInfo === null) { ...build default connection... } return self::$defaultConnectionInfo; } } class SearchHelper extends AjaxACApplication { private $dbh; public function __construct(DatabaseConnectionInfo $connectionInfo = null) { if ($connectionInfo === null) { $connectionInfo = DatabaseConnectionInfo::getDefault(); } $this->dbh = $connectionInfo->getHost(); } } Yes, I know what you're thinking, too much more code for just a stupid thing. Trust me, refactoring it's easier when the code was written right the first time. 2007/9/30, 潘志彬 <[EMAIL PROTECTED]>: > > $dbh = 'test'; > > class search_helper extends AjaxACApplication > { > /** > * Database connection details > */ > >// announce global variable before use it >global $dbh; > > $db_hostname = $dbh; > > >... > > Regards, > Ryu > > > > 2007/9/29, Merlin <[EMAIL PROTECTED]>: > > > > Hi there, > > > > I am new to PHP classes and I do want to access a variable outside the > > class, but somehow that does not work. global also does not have any > > effect. > > > > In the following example I would like to be able to access $dbh from > > inside the class like I did in that example. This does not work. Can > > somebody please give me a hint on the right syntax? > > > > $dbh = 'test'; > > > > class search_helper extends AjaxACApplication > > { > > /** > > * Database connection details > > */ > > > > var $db_hostname = $dbh; > > > > Thank you for any help, > > > > Merlin > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > >
Re: [PHP] Classes and access to outside variables
On 9/30/07, 潘志彬 <[EMAIL PROTECTED]> wrote: > > $dbh = 'test'; > > class search_helper extends AjaxACApplication > { > /** > * Database connection details > */ > >// announce global variable before use it >global $dbh; > > $db_hostname = $dbh; global variables inside a class ?? as Martin said this is a bad practice. if you were going to use a global variable inside a class, why bother w/ a class in the first place? the point is to encapsulate, ie. hide implementation details from the user. by relying on a global variable, the class is open to behavior modification by external code that does not have to go through its well-defined public interface to do so. thats why its a bad practice. -nathan
Re: [PHP] Classes and access to outside variables
$dbh = 'test'; class search_helper extends AjaxACApplication { /** * Database connection details */ // announce global variable before use it global $dbh; $db_hostname = $dbh; ... Regards, Ryu 2007/9/29, Merlin <[EMAIL PROTECTED]>: > > Hi there, > > I am new to PHP classes and I do want to access a variable outside the > class, but somehow that does not work. global also does not have any > effect. > > In the following example I would like to be able to access $dbh from > inside the class like I did in that example. This does not work. Can > somebody please give me a hint on the right syntax? > > $dbh = 'test'; > > class search_helper extends AjaxACApplication > { > /** > * Database connection details > */ > > var $db_hostname = $dbh; > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] Classes and access to outside variables
Merlin, if you are using php5 the var keyword is no longer valid; that was used in classes in php4. if you want to structure your classes properly you need to study PPP (public, private, protected) access modifiers. typically member variables are declared to be private or protected, if there will be child objects that will access them directly. to provide access to said member variables creates public method. public methods are visible to external code and have access to the private members, be they variables or methods. -nathan On 9/29/07, Merlin <[EMAIL PROTECTED]> wrote: > > Hi there, > > I am new to PHP classes and I do want to access a variable outside the > class, but somehow that does not work. global also does not have any > effect. > > In the following example I would like to be able to access $dbh from > inside the class like I did in that example. This does not work. Can > somebody please give me a hint on the right syntax? > > $dbh = 'test'; > > class search_helper extends AjaxACApplication > { > /** > * Database connection details > */ > > var $db_hostname = $dbh; > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] Classes and access to outside variables
Refer to the global on the constructor. Anyway, using a global like that is not a good practice. Just pass the variable to the constructor. 2007/9/29, Merlin <[EMAIL PROTECTED]>: > > Hi there, > > I am new to PHP classes and I do want to access a variable outside the > class, but somehow that does not work. global also does not have any > effect. > > In the following example I would like to be able to access $dbh from > inside the class like I did in that example. This does not work. Can > somebody please give me a hint on the right syntax? > > $dbh = 'test'; > > class search_helper extends AjaxACApplication > { > /** > * Database connection details > */ > > var $db_hostname = $dbh; > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
[PHP] Classes and access to outside variables
Hi there, I am new to PHP classes and I do want to access a variable outside the class, but somehow that does not work. global also does not have any effect. In the following example I would like to be able to access $dbh from inside the class like I did in that example. This does not work. Can somebody please give me a hint on the right syntax? $dbh = 'test'; class search_helper extends AjaxACApplication { /** * Database connection details */ var $db_hostname = $dbh; Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php