473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to call different methods in one PHP file?

I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate
PHP file to support each HTML PHP page. This would be the equivalent of an
ASP.NET code-behind file but using PHP.

For one page to do a variety of different things I need to call different
methods (or functions) in the PHP code file that is dedicated to this purpose.
How do you do this? How do you call just one method?

Thanks for your help.
Jul 17 '05 #1
7 5160
With total disregard for any kind of safety measures "Bruce W...1"
<br***@noDirectEmail.com> leapt forth and uttered:
I'm a PHP newbie coming from experience with ASP.NET. I want to
have a separate PHP file to support each HTML PHP page. This
would be the equivalent of an ASP.NET code-behind file but using
PHP.

For one page to do a variety of different things I need to call
different methods (or functions) in the PHP code file that is
dedicated to this purpose. How do you do this? How do you call
just one method?

Thanks for your help.


I'm not entirely sure what you're asking, if you have a class you
have the choice of either instantiating it as an object and calling
the method on the object like so:

$object =& new ClassName($class_parameters_if_you_have_any);
$object->methodName();

Or if the class is composed of static (stand-alone) methods it can be
called as:

ClassName::methodName();

--
There is no signature.....
Jul 17 '05 #2
"Bruce W...1" <br***@noDirectEmail.com> schrieb:
For one page to do a variety of different things I need to call different
methods (or functions) in the PHP code file that is dedicated to this purpose.
How do you do this? How do you call just one method?


According to the parameters of the page.

If I call my PHP page with example.php?task=2 the code in example2.php
will lokk like that:
$task = (isset($_GET['task']) ? intval($_GET['task']) : 0;
switch ($task) [
case 1:
functionA();
break;
case 2:
functionB();
break;
default:
break;
}

Regards,
Matthias
Jul 17 '05 #3

"Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:3F**************@noDirectEmail.com...
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP.

For one page to do a variety of different things I need to call different
methods (or functions) in the PHP code file that is dedicated to this purpose. How do you do this? How do you call just one method?

Thanks for your help.


It isn't good programming style to use one PHP file per HTML page. Classes
should be based on functionality, as should PHP files. If all your pages
were to do with a membership system then you should have a Member class with
methods appropriate
Jul 17 '05 #4
"Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:3F**************@noDirectEmail.com...
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP.

For one page to do a variety of different things I need to call different
methods (or functions) in the PHP code file that is dedicated to this purpose. How do you do this? How do you call just one method?

Thanks for your help.


It isn't good programming style to use one PHP file per HTML page. Classes
should be based on functionality, as should PHP files. If all your pages
were to do with a membership system then you should have a Member class with
methods appropriate to that functionality.

If you've got a group of functions that perform similar actions you should
include them in a functions.member.inc.php file and include them where you
need them. You should group functions/methods by functionality rather than
per page as it will reduce the repetition element, imo.

Paulus
Jul 17 '05 #5
"Matthias Esken" <mu******************@usenetverwaltung.org> wrote in
message news:bl**********@usenet.esken.de...
$task = (isset($_GET['task']) ? intval($_GET['task']) : 0;
switch ($task) [
This should be:
switch ($task) {
case 1:
functionA();
break;
case 2:
functionB();
break;
default:
break;
}

Jul 17 '05 #6
Paulus Magnus wrote:

It isn't good programming style to use one PHP file per HTML page. Classes
should be based on functionality, as should PHP files. If all your pages
were to do with a membership system then you should have a Member class with
methods appropriate to that functionality.

If you've got a group of functions that perform similar actions you should
include them in a functions.member.inc.php file and include them where you
need them. You should group functions/methods by functionality rather than
per page as it will reduce the repetition element, imo.

Paulus


================================================== ==============

That makes sense I guess, if the functions are used by more than one page.

That still doesn't answer my original question but thats okay.

I'm also trying to make sense out of the file names. In a system where every
file can be named whatever.php there needs to be some organization. So I was
going to have a codebehind file for each page named whatever.cb.php.

May I ask what are your file naming conventions? functions.member.inc.php is
one of them.
Jul 17 '05 #7
"Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:3F**************@noDirectEmail.com...
Paulus Magnus wrote:

It isn't good programming style to use one PHP file per HTML page. Classes should be based on functionality, as should PHP files. If all your pages
were to do with a membership system then you should have a Member class with methods appropriate to that functionality.

If you've got a group of functions that perform similar actions you should include them in a functions.member.inc.php file and include them where you need them. You should group functions/methods by functionality rather than per page as it will reduce the repetition element, imo.

Paulus
================================================== ==============

That makes sense I guess, if the functions are used by more than one page.

That still doesn't answer my original question but thats okay.

I'm also trying to make sense out of the file names. In a system where

every file can be named whatever.php there needs to be some organization. So I was going to have a codebehind file for each page named whatever.cb.php.

May I ask what are your file naming conventions? functions.member.inc.php is one of them.


I tend to put my classes in a single file called class.name.php and place
them in a /classes directory on the site. I then usually have two other
files in the site root, globals.inc.php and common.inc.php. Common.inc.php
contains all the basic functions I use on multiple pages and also includes
globals.inc.php. Globals.inc.php is just a list of variables and constants
for the site that contains MySQL information, site URL and paths, etc. Every
PHP file on the site then includes common.inc.php, which in turn includes
globals.inc.php.

I don't use any external files for functions. If a function is required
across pages I write a class otherwise it sits in the actual page. I also
don't have any HTML in my PHP pages, they're pure PHP and all the template
stuff is in a collection of tpl.name.php files that the pages include as
necessary. Separating the design from the logic is a big time saver and big
bug reducer.

Going back to your original question...

To call a method from an included file you need to include it, instantiate
the class and then call the method;

require ("class.name.php");
$myclass = new FunkyClass;
$myclass->method_name ();

If the class was in the same PHP file you wouldn't need the require
statement.

If you wanted to call a function from an included file you need to include
it and call it;

require ("functions.member.php");
funky_function ();

And same again, if the function is in the same PHP file, no require
statement is needed.

Paulus
Jul 17 '05 #8

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

Similar topics

2
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to...
0
by: ding feng | last post by:
I have got formatted files. All of these file takes this form: obj1 method1 obj2 string1 obj3 string2 argument. Of course, each file can have maximum fields as shown above, or minimun fields...
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
6
by: Tim | last post by:
I have a vb6 Exe. I need to call a C# dll's methods from the VB Exe. I have used RegAsm to register the C# dll. If the VB6 Exe and the C# dll are in the same folder, then everything is fine. But I...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.