richardstan Posted September 19, 2008 Share Posted September 19, 2008 Hi, I want to be able to construct and use the methods of a class in one php file from another php file. For example Database.php class MySQL{ function connect($hostname,$pass,$user) { return $link; } function disconnect($link) {} } then use these methods in another php form. How do i go about doing this? Thanks Richard. Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/ Share on other sites More sharing options...
corbin Posted September 19, 2008 Share Posted September 19, 2008 In PHP you have to define the entire class at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/#findComment-645961 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 Use the http://us3.php.net/manual/en/function.include.php function. Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/#findComment-645962 Share on other sites More sharing options...
wildteen88 Posted September 19, 2008 Share Posted September 19, 2008 include database.php in the other file you want to use the class then initiate a new instance of the class, eg include 'Database.php'; // create a new instance of the MySQL class $mysql = new MySQL; $mysql->connect('localhost', 'user', 'pass'); $mysql->close(); Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/#findComment-645965 Share on other sites More sharing options...
corbin Posted September 19, 2008 Share Posted September 19, 2008 Oh wow I misread that post entirely. I thought he wanted to define the class then define the disconnect function later. Brain dead moment. Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/#findComment-645967 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 You could also include, and then extend your class to include the other members. Quote Link to comment https://forums.phpfreaks.com/topic/125008-referencing-classes-and-methods-from-separate-php-file/#findComment-645989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.