Accessing Class Method

This is a discussion on Accessing Class Method within the PHP Language forums, part of the PHP Programming Forums category; I have the following code: index.php: class main_class { public $database = new DAL; public $html = new HTML; } dal.php: class ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-17-2007
RageARC
 
Posts: n/a
Default Accessing Class Method

I have the following code:

index.php:
class main_class {

public $database = new DAL;
public $html = new HTML;

}

dal.php:
class DAL {

function method() {
# Code
}

}

class HTML {

function method() {
# Code
# HOW TO CALL DAL'S METHOD?
# Code
}

}

As you see, DAL and HTML are inside two variables of two classes, but
how can they interact with each other?

Reply With Quote
  #2 (permalink)  
Old 09-17-2007
NoDude
 
Posts: n/a
Default Re: Accessing Class Method

They can't interact, because they'll never be able to see each other.
If you want to accomplish an interaction using your current setup,
you'll have to write methods in the main_class class, which will
delegate information from one object to another.

On Sep 17, 3:13 pm, RageARC <rage...@gmail.com> wrote:
> I have the following code:
>
> index.php:
> class main_class {
>
> public $database = new DAL;
> public $html = new HTML;
>
> }
>
> dal.php:
> class DAL {
>
> function method() {
> # Code
>
> }
> }
>
> class HTML {
>
> function method() {
> # Code
> # HOW TO CALL DAL'S METHOD?
> # Code
>
> }
> }
>
> As you see, DAL and HTML are inside two variables of two classes, but
> how can they interact with each other?




Reply With Quote
  #3 (permalink)  
Old 09-17-2007
RageARC
 
Posts: n/a
Default Re: Accessing Class Method

What if I statically call the other class function?

index.php:
include ('dal.php');
include ('html.php');
class main_class {

public $database = new DAL;
public $html = new HTML;

}

dal.php:
class DAL {

function method() {
# Code

}
}

html.php:
class HTML {

function method() {
# Code
DAL::method();
# Code

}
}

Does this code work???

Reply With Quote
  #4 (permalink)  
Old 09-17-2007
Steve
 
Posts: n/a
Default Re: Accessing Class Method

"RageARC" <ragearc@gmail.com> wrote in message
news:1190039567.080009.174300@o80g2000hse.googlegr oups.com...
> What if I statically call the other class function?


what if? there was nothing *static* in your example revision. main_class
could be instanciated numerous times, each with its own isolated versions of
DAL and HTML...neither of which are descriptive of what they are/do, btw.

> index.php:
> include ('dal.php');
> include ('html.php');


well, first you'd need to *require* rather than include, and that only
*once*...and each class definition would need to do this rather than just
index.php. finally, what kind of name is main_class? that describes nothing
in a system that does many things. anyway, your class would look something
like:

<?
require_once 'dal.php';
require_once 'html.php';
class main_class
{
public static $db = null;
public static $html = '';
private function __contstruct()
{
self::$db = new DAL();
self::$html = new HTML();
}
public static function initialize(){};
}
?>

> Does this code work???


work? dunno, did you test it? ;^)

this should:

index.php

<?
require_once 'main.vaguely.named.class.php';
main_class::initialze();
print_r(main_class::$db);
print_r(main_class::$html);
?>


Reply With Quote
  #5 (permalink)  
Old 09-17-2007
RageARC
 
Posts: n/a
Default Re: Accessing Class Method

Well, Steve, I called it main_class just for an example purpose. The
fact is this is supposed to be a class that holds several class in
only one, so I could call:

$main_class->database->method($args);
$main_class->html->make_form($args);

Understood? I think this is much better than having several variables
for different classes, as it groups the whole functionality of all
classes into only one variable. If you find there is a way to do this
better, please, do tell me.

Reply With Quote
  #6 (permalink)  
Old 09-17-2007
Steve
 
Posts: n/a
Default Re: Accessing Class Method


"RageARC" <ragearc@gmail.com> wrote in message
news:1190049520.484592.326400@57g2000hsv.googlegro ups.com...
> Well, Steve, I called it main_class just for an example purpose. The
> fact is this is supposed to be a class that holds several class in
> only one, so I could call:
>
> $main_class->database->method($args);
> $main_class->html->make_form($args);


if main is static, only the syntaxt would change:

main::db->method

if dal is static as well, then:

main::db::interface();


> Understood? I think this is much better than having several variables
> for different classes, as it groups the whole functionality of all
> classes into only one variable. If you find there is a way to do this
> better, please, do tell me.


true, however if that's the only way you allow access to them, then you
loose the ability to reuse part or all of their functionality. that may be
no big deal in reality, and i can't tell from your example, however that
should always be a consideration when making such choices in design.

i did neglect to see your html code where it consumes dal. you'd want to
require_once 'dal.php'. whether or not you make dal or html statically
interfaced is up to you. nothing says you can't create a dal when html is
constructed. the main class c/would kick all of that off when you
constructed it via the initialize() function. that would allow you to reuse
the dal and html classes independently but still get the result you're
looking for when called via the static main_class object. clear as mud?

make sense?

btw, i was giving you a hard time about the names...no biggie.


Reply With Quote
  #7 (permalink)  
Old 09-17-2007
RageARC
 
Posts: n/a
Default Re: Accessing Class Method

Well I wouldn't lose reusability as all the classes are supposed to
work together for main_class, thus providing a set of functions
available in one single variable. It is built to be that way.

I think I am gonna use that code I have posted. Seems to be better.
And I never called my class functions static and I still could call
them statically :S.

Reply With Quote
  #8 (permalink)  
Old 09-17-2007
NoDude
 
Posts: n/a
Default Re: Accessing Class Method

By the looks of it, you're looking for the singleton pattern.

final class Singleton{
private static $sql;
private static $dao;
private static $smarty;

public static function getSql(){
if(!isset(self::$sql)){
require CLASS_DIR.'Sql.php';
self::$sql = new Sql;
}
return self::$sql;
}

public static function getDao(){
if(!isset(self::$dao)){
require CLASS_DIR.'Dao.php';
self::$dao = new Dao;
}
return self::$dao;
}

public static function getSmarty(){
if(!isset(self::$smarty)){
require SMARTY_DIR.'Smarty.class.php';
self::$smarty = new Smarty;
//some config stuff
}
return self::$smarty;
}

//prevent construction/cloning
public function __construct(){
trigger_error("Singleton instantiation not allowed", E_USER_ERROR);
}

public function __clone(){
trigger_error("Singleton cloning not allowed", E_USER_ERROR);
}
}

Btw. Try to write an OO application in a manner that would not require
the require_once statement, but rather a plain require. In part,
because it's faster, but mainly because it will force you to write
your applications in a much straightforward manner. Maybe
straightforward isn't the word here, but I'm tired as hell...

On Sep 17, 9:18 pm, RageARC <rage...@gmail.com> wrote:
> Well I wouldn't lose reusability as all the classes are supposed to
> work together for main_class, thus providing a set of functions
> available in one single variable. It is built to be that way.
>
> I think I am gonna use that code I have posted. Seems to be better.
> And I never called my class functions static and I still could call
> them statically :S.



Reply With Quote
  #9 (permalink)  
Old 09-17-2007
Steve
 
Posts: n/a
Default Re: Accessing Class Method

> Btw. Try to write an OO application in a manner that would not require
> the require_once statement, but rather a plain require. In part,
> because it's faster, but mainly because it will force you to write
> your applications in a much straightforward manner. Maybe
> straightforward isn't the word here, but I'm tired as hell...


if both main_class and html require dal.php and could be used in the same
script, require_once is better and produces less parsing in php...and is
therefore faster than php having to check if it loaded a file already or
not. THAT is straightforward...probably not the word you're looking for.


Reply With Quote
  #10 (permalink)  
Old 09-17-2007
NoDude
 
Posts: n/a
Default Re: Accessing Class Method

I think you're confusing require_once with require. require_once has
to check if a file has been loaded or not, require does a
straightforward require - hence the speed boost.

About writing straightforward scripts. Using require instead of
require_once will cause php to throw an error the second you try to
redefine a class you already have (which will happen if you require a
file with the same class declaration twice). By all all means, use
require_once, it's a language statement, it's useful when you need to
use it, BUT a thought out OOP should _not_ need the *_once statements.
The flow of an OOP using a robust design paradigm (like MVC for
instance), should allow for full knowledge of which class gets
included where, if not you're looking at a need for a singleton, if
that doesn't help - delegate to the method required.

Like I said, I'm not imposing the sole usage of require over require
once (although I prefer it), I'm also not saying you should rewrite
existing code to gain the 0.xx ms advantage of require_once over
require.

What I _am_ saying is that using plain require over require once has
solid benefits. And heck, if you can whine about his naming
conventions, why can't I about your coding style :)

On Sep 17, 10:36 pm, "Steve" <no....@example.com> wrote:
> > Btw. Try to write an OO application in a manner that would not require
> > the require_once statement, but rather a plain require. In part,
> > because it's faster, but mainly because it will force you to write
> > your applications in a much straightforward manner. Maybe
> > straightforward isn't the word here, but I'm tired as hell...

>
> if both main_class and html require dal.php and could be used in the same
> script, require_once is better and produces less parsing in php...and is
> therefore faster than php having to check if it loaded a file already or
> not. THAT is straightforward...probably not the word you're looking for.



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0