473,772 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Sep 17 '07
29 1684
On Sep 17, 10:36 pm, "Steve" <no....@example .comwrote:
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.

"NoDude" <no****@gmail.c omwrote in message
news:11******** *************@2 2g2000hsm.googl egroups.com...
>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.

no, i'm thinking of the correct thing. and it entirely depends on what your
errors are set to before you'll get that error. i'm not a noob and i know
when and why to use either. there is NO significant speed boost using
either, but a world of problems in an oop context by not using one.
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.
'thought' out? that's bullshit. if you are producing truly loosely coupled
objects, you will inevitably be loading class files (requiring) where you'd
likely produce the errors because of an object's reuse! require_once is a
FORMAL means that averts those errors. if you are THINKING about oop and
thinking CLEARLY, you'd notice the pattern lends itself more to *_once than
include/require alone!

you produce a time-tested (as in speed differencial) benefit between require
and require_once that show a significant load difference, and i'll be very
suprised. either way, show me your mechanism that allows loose coupling,
loads any required resource without one class knowing whether or not another
resource is consumed by another caller/object, and is as easily implemented
as *_once, and then we'll have something to talk about.
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.

sorry, php is far from a robust design paradigm when it comes to oop
(comparatively) , so for now, require_once is a one-line, quick means to load
a resource without having to allow for 'full knowledge' - which is ANTI-OOP
anyway.
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.
design v. time where time == 1 nano-second. hmmm...design wins.
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 :)
you have no idea what my coding style is. when dealing with class files that
are to be used within other class files, it is ALWAYS the best option to
require_once.

why can't you? well, first, because you don't know it. second, your
reasoning is not sound. that's why i wouldn't...but you go right ahead.

btw, why are you top-posting?
Sep 17 '07 #11
..oO(NoDude)
>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
Consider two entirely independent classes A and B, both relying or
inheriting from another class C. Since both A and B should be usable
without each other, both have to take care of their own dependencies and
hence have to include class C (unless you use __autoload()).

No problem with require_once, but if you just use require and want to
use both classes in the same project, you'll run into trouble.

Micha
Sep 17 '07 #12
@Steve - The term "coding style" was a very poor choice of words, what
I was referring to was require over require_once. My initial reasoning
(for myself_ to use require over requireonce came from an early
release of APC which had big issues with require_once. Maybe I'm
justifying my usage of require, claiming it to be better, but that's
how I see things. I didn't mean to criticize an y of your work (which
I've never seen anyway).

@Michael - I currently use __autoload, which is a neat shortcut,
albeit it has the same speed impact as *_once (in my case, even
greater, because of directory traversing). I did however use an AS3-
like import mechanism while I was writing php4 code. I also had
preconfigured dependencies for most of the models and pageControllers
in my applications (the total preconfiguratio ns were not that
numerous).

How I (or Steve for that matter) include our files is not (and never
was) my point however. I was just saying and still am - Using require
over require_once makes you think of what dependencies you'll have in
any given request (every single request is unaware of the dependencies
in the previous request and has its own dependencies). It's true that
this kind of approach makes you think in terms of configuration rather
than automation (not sure if that's the word), but having to make a
delegator in every single controller (for example), makes you think
hard about what you're doing wrong (in some cases it just pisses you
off).

Let me also state that I'm a fan of automation (I would much rather
have cakePHP over symfony for example), I even use lazy loading in my
aplications. I would be nuts if I had a require 'something.php' all
over the place (which I once had, years ago).

However, I do think that making someone structure his own dependencies
(or using some method to overcome having to define them) will make him
_think_ in terms of an application, instead of a collection of
objects. Telling him, he can have a dozen files, each one having a
bunch of require_onces for all of his dependent files won't get him
much farther than continuing with the procedural style thinking, only
with objects as capsules for functions. I've seen this kind of
thinking in at least half a dozen colleagues, also in myself when I
was starting out with OOP using php (which was not very OO at the
time).

P.S. I was top-posting, because I was under the impression this is the
preferred method in this group.

Sep 18 '07 #13
Wow Usenet Groups rule this is better than a forum :D

---

I tried to test some code and here's the result:

INDEX.PHP

<?php
function __autoload($cla ss_name) {
require_once $class_name . '.php';
}

class container {

public $database;
public $html;

function __construct() {
$this->database = new database;
$this->html = new html;
}

}

$cont = new container();
$cont->html->access_db();
?>

DATABASE.PHP:

<?php
class database {

function something() {
return 'something';
}
}
?>

HTML.PHP:

<?php
class html {

function access_db() {
print database::somet hing();
}
}
?>

It works, 100% tested ;). As you see, HTML is statically using a
method from DATABASE without any static keywords or anything. Just
code. I had already used this method before, and I didn't remember it.
I only remembered now xD.

Sep 18 '07 #14
RageARC wrote:
Wow Usenet Groups rule this is better than a forum :D

---

I tried to test some code and here's the result:

INDEX.PHP

<?php
function __autoload($cla ss_name) {
require_once $class_name . '.php';
}

class container {

public $database;
public $html;

function __construct() {
$this->database = new database;
$this->html = new html;
}

}

$cont = new container();
$cont->html->access_db();
?>

DATABASE.PHP:

<?php
class database {

function something() {
return 'something';
}
}
?>

HTML.PHP:

<?php
class html {

function access_db() {
print database::somet hing();
}
}
?>

It works, 100% tested ;). As you see, HTML is statically using a
method from DATABASE without any static keywords or anything. Just
code. I had already used this method before, and I didn't remember it.
I only remembered now xD.
Yes, it works now. But that doesn't mean this hole won't be fixed in
future releases.

If you're calling a method statically, it needs to be defined as a
static method. Otherwise you have a bug waiting to happen.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 18 '07 #15
Ahem... I have a feeling I'll get shot, stabbed and hung for this,
buuuuuut... There's really no need for require_once in __autoload,
because if you've reached the __autoload function, the class is
obviously not present (no sense making php check for it a second
time). Also, if you're including from the same directory, use './'.
$class_name.'.p hp', that way php won't look in the includes paths.

Sep 18 '07 #16

"Jerry Stuckle" <js*******@attg lobal.netwrote in message
news:sZ******** *************** *******@comcast .com...
RageARC wrote:
>Wow Usenet Groups rule this is better than a forum :D

---

I tried to test some code and here's the result:

INDEX.PHP

<?php
function __autoload($cla ss_name) {
require_once $class_name . '.php';
}

class container {

public $database;
public $html;

function __construct() {
$this->database = new database;
$this->html = new html;
}

}

$cont = new container();
$cont->html->access_db();
?>

DATABASE.PHP :

<?php
class database {

function something() {
return 'something';
}
}
?>

HTML.PHP:

<?php
class html {

function access_db() {
print database::somet hing();
}
}
?>

It works, 100% tested ;). As you see, HTML is statically using a
method from DATABASE without any static keywords or anything. Just
code. I had already used this method before, and I didn't remember it.
I only remembered now xD.

Yes, it works now. But that doesn't mean this hole won't be fixed in
future releases.

If you're calling a method statically, it needs to be defined as a static
method. Otherwise you have a bug waiting to happen.
amen.
Sep 18 '07 #17

"NoDude" <no****@gmail.c omwrote in message
news:11******** *************@y 42g2000hsy.goog legroups.com...
Ahem... I have a feeling I'll get shot, stabbed and hung for this,
buuuuuut... There's really no need for require_once in __autoload,
because if you've reached the __autoload function, the class is
obviously not present (no sense making php check for it a second
time). Also, if you're including from the same directory, use './'.
$class_name.'.p hp', that way php won't look in the includes paths.
not shot. ;^)

it is best not to assume *anything* in programming, but be explicit all the
time. i use a config file that gets included in *every* script i write.
guess what? each script gets required once...anyway, i digress. that config
file objectifies my site's properties including path information
(site::includeD irectory)...tha t avoids many problems like not having to keep
the same directory structure when released to a different server and having
to reprogram for the new env. it also eliminates the problem you mention
with traversing include paths. other benefits too, but the main one being
one location to manage paths without being tied to relativism.

that's just my 0.02 usd.
Sep 18 '07 #18
..oO(NoDude)
>@Michael - I currently use __autoload, which is a neat shortcut,
albeit it has the same speed impact as *_once (in my case, even
greater, because of directory traversing).
I also traverse a lot of class directories, but only if the requested
class could not be found in the class cache, where the locations of all
classes are stored. In such case the cache has to be refreshed.
>How I (or Steve for that matter) include our files is not (and never
was) my point however. I was just saying and still am - Using require
over require_once makes you think of what dependencies you'll have in
any given request (every single request is unaware of the dependencies
in the previous request and has its own dependencies).
Knowing beforehand which classes will be required to handle a particular
request is pretty much impossible in my framework. The request handlers
themselves decide which of them will be responsible for answering the
request and which other objects might be necessary for doing that. It's
even possible that a handler instantiates some objects and then forwards
the request to a sub handler, which in turn might need the informations
provided by the parent handler.
>It's true that
this kind of approach makes you think in terms of configuration rather
than automation (not sure if that's the word), but having to make a
delegator in every single controller (for example), makes you think
hard about what you're doing wrong (in some cases it just pisses you
off).
I think more about modularization and code separation. Each component is
an independent thing and takes care of its own dependencies.
>However, I do think that making someone structure his own dependencies
(or using some method to overcome having to define them) will make him
_think_ in terms of an application, instead of a collection of
objects. Telling him, he can have a dozen files, each one having a
bunch of require_onces for all of his dependent files won't get him
much farther than continuing with the procedural style thinking, only
with objects as capsules for functions.
Ever written Java programs? A typical Java class often starts with a
whole bunch of 'import' statements. Of course a 'require_once' is not
the same, but quite similar (IMHO).

Micha
Sep 18 '07 #19
NoDude wrote:
Ahem... I have a feeling I'll get shot, stabbed and hung for this,
buuuuuut... There's really no need for require_once in __autoload,
because if you've reached the __autoload function, the class is
obviously not present (no sense making php check for it a second
time). Also, if you're including from the same directory, use './'.
$class_name.'.p hp', that way php won't look in the includes paths.
No, you're correct, there's no reason to use require_once if you use
autoload.

OTOH, while require_once means you need to add another statement to your
code, the execution is faster. And it will work on any system - i.e. a
shared host with autoload disabled.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 18 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3915
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
6
5640
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
5
2416
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
8
3328
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables in its various methods. The problem I am having is creating an instance of this class when it is called via SOAP. I don't seem to have access to the constructor method or any class variables... is this by design? Must all methods be static? Here is my sample code: This is a very dumbed...
5
2809
by: Khalique | last post by:
Hi everyone, I Hope that someone will be able to give me a hint to the solution to my problem. I have developed a web service (vb.net) that needs to access the folders / files and copy files to and from the public folder on the client machine. It is not a public web service, only accessible on intranet. The anonymous access is disabled. Windows authentication is enabled. Web.Config sets <identity impersonate="true" /> Using a test app,...
3
5006
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : >************************************************************************ > <WebMethod(), System.Web.Services.Protocols.SoapRpcMethod()> _ > Public Function HelloWorld() As > <System.Xml.Serialization.SoapElementAttribute("return")> String
5
2404
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the correct value. Regardless what I set the variable to, the value that is returned for a long is always the same value. What's going on...can anyone help me? A short version of the code follows:
3
4605
by: Jeff | last post by:
Hey asp.net 2.0 In the source I posted below, there is a GridView (look at the bottom of the script): <asp:GridView ID="gvwOnline" runat="server"> </asp:GridView> I'm trying to assign a datasource to this GridView in runtime. But I cannot
9
2651
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method to another location because it populates a Textbox in the user control page. Thanks Andrew
0
2032
by: Roger Stoller | last post by:
Hello. I have developed a COM object using ATL. It seems to work fine when accessing it from VB.NET most of the time. However, I want to use a delegate in VB to asynchronously run a method in one of the interfaces in my COM module (using Delegate.BeginInvoke()). The interface seems to be "junk" when accessing it from the delegated VB method. When called synchronously from the same thread using the Delegate.Invoke(), it seems to work...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.