Hi!
I coded with php few programs awhile ago. Then I switched to java servlets, witch I like very much. It's faster, more reliable etc. (primarily because of oop requirements; you have to code better and more logically...) However, there are some parts that are unnecceseraly painfull to implement in java (Like file downloads, also atleast with O8i DBMS there are tons of problems with the Oracle thin driver etc.) Now I'm somewhat ready give php a second go.
And the questions:
1. How do I differentiate public and private methods in php? (and variables)
2. Can I pass a class (instance of a class) to a method in php?
Thanks
-9902468
oop and php
Moderator: General Moderators
Quite frankly, I'm unsure about the private variables thing, but calling a class from a separate function should be no problem.
Code: Select all
function call_class() {
$login = new LOGIN;
$login -> start_login();
}I meant that can I do this:Jim wrote:Quite frankly, I'm unsure about the private variables thing, but calling a class from a separate function should be no problem.
Code: Select all
function call_class() { $login = new LOGIN; $login -> start_login(); }
Code: Select all
$checker = new check_things();
$login = new login_($username, $password);
$checker->is_password_ok($login);Ofcourse I could write
Code: Select all
$checker->is_password_ok($login->get_username, $login->get_password);Here is the imaginary check_things classes is_password_ok method:
Code: Select all
function is_password_ok($login_data){
$login_data->AND_NOW_YOU_CAN_ACCESS_LOGINS_METHODS;
}If you create childs (extends - see manual php.net) the child class can access all the properties and methods of the parent. Don't forget that the parent constructor has to be explicitly called by child.
There are also aggregate fns which I haven't delved into yet - again see the manual for more info.
Class methods can't be shared unless you have parent child relationships - but then methods create vars somewhere along the line which you can get at easily. So, in your example, you would (I think) have this:
..although it sounds like there would be a child/parent relationship here, so you'd maybe have this:
Info on the next release of php can be found on php.net: lots of new OOP stuff. Fine if you run your own server I guess but it will probably be years before it's available on the shared hosts.
I couldn't say how well php OOP compares to other languages but it works for me.
There are also aggregate fns which I haven't delved into yet - again see the manual for more info.
Class methods can't be shared unless you have parent child relationships - but then methods create vars somewhere along the line which you can get at easily. So, in your example, you would (I think) have this:
Code: Select all
<?php
$checker = new check_things();
// constructor sets a $success_value property
$login = new login_($username, $password);
// is_password_ok method takes $login->success_value as an arg
$checker->is_password_ok($login->success_value);
?>Code: Select all
<?php
// instantiate child (& parent)
$checker = new check_things();
// run parent constructor
$checker->login_($username, $password);
// get success_value (child method has access to properties set by parent)
$checker->is_password_ok();
$success_value = $checker->getSuccess();
// wrap that up in a fn (isMember - return a value 0 or 1) and you have a simple little script killer:
IF (!isMember($username, $password)) {
die('Name & pass not recognised.');
}
// .. or carry on with the script
?>I couldn't say how well php OOP compares to other languages but it works for me.
Last edited by McGruff on Thu Aug 11, 2005 7:56 am, edited 3 times in total.