PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Accessing Class Method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Accessing Class Method

Réponse
 
LinkBack Outils de la discussion
Vieux 17/09/2007, 13h13   #1
RageARC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?

  Réponse avec citation
Vieux 17/09/2007, 14h16   #2
NoDude
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?




  Réponse avec citation
Vieux 17/09/2007, 15h32   #3
RageARC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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???

  Réponse avec citation
Vieux 17/09/2007, 17h01   #4
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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);
?>


  Réponse avec citation
Vieux 17/09/2007, 18h18   #5
RageARC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 17/09/2007, 18h51   #6
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 17/09/2007, 19h18   #7
RageARC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 17/09/2007, 20h06   #8
NoDude
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h28.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12631 seconds with 16 queries