Page 1 of 1

oop and php

Posted: Tue May 27, 2003 6:43 am
by 9902468
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

Posted: Tue May 27, 2003 7:32 am
by Jim
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();

}

Posted: Tue May 27, 2003 8:04 am
by 9902468
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();

}
I meant that can I do this:

Code: Select all

$checker = new check_things();
$login = new login_($username, $password);

$checker->is_password_ok($login);
Eg. give an object as parameter to another class/function. This is extremely helpful.

Ofcourse I could write

Code: Select all

$checker->is_password_ok($login->get_username, $login->get_password);
but that is not very good way to code.

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;
}
This crants you the privilege to alter the amount of parameters passed to your functions/methods. (You can change login and check_things classes - and they are still compatible with the code that was written before your update!!! No need to change those xxx function calls in your programs.)

Posted: Tue May 27, 2003 8:25 am
by McGruff
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:

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); 
?>
..although it sounds like there would be a child/parent relationship here, so you'd maybe have this:

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

?>
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.

Posted: Tue May 27, 2003 8:43 am
by 9902468
Thanks for your input. It seems that it is propably good if I pretty much forget what I did in java and start from scratch w/php... So, I'm of to reading some manuals. (The best hobby (tm) in the world!!)

-9902468