473,320 Members | 1,909 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,320 software developers and data experts.

classes and require ?

Hello

I have a big class in one php file.
I would like to divide it to many smaller files. But when i try:

<?php

class IndexController extends Zend_Controller_Action
{
require_once('IndexController_common.php');
require_once('IndexController_client_car.php');

i receive parse error:
"Parse error: syntax error, unexpected T_REQUIRE_ONCE, expecting
T_FUNCTION in
/mnt/hda5/www/zend/dg/car/application/controllers/IndexController.php on
line 6"

When i move require_once outside class definition everything works fine.
But i need require inside
of class because in thouse files are functions which are the member of
that class.
How can i do it ?

Thanx
Mar 12 '07 #1
3 15061
avlee <xx@wp.plwrites:
Hello

I have a big class in one php file.
I would like to divide it to many smaller files. But when i try:

<?php

class IndexController extends Zend_Controller_Action
{
require_once('IndexController_common.php');
require_once('IndexController_client_car.php');

i receive parse error:
"Parse error: syntax error, unexpected T_REQUIRE_ONCE, expecting
T_FUNCTION in
/mnt/hda5/www/zend/dg/car/application/controllers/IndexController.php
on line 6"

When i move require_once outside class definition everything works
fine. But i need require inside
of class because in thouse files are functions which are the member of
that class.
How can i do it ?
Put them in __construct()
--
Henrik Hansen
Mar 12 '07 #2
avlee wrote:
Hello

I have a big class in one php file.
I would like to divide it to many smaller files. But when i try:

<?php

class IndexController extends Zend_Controller_Action
{
require_once('IndexController_common.php');
require_once('IndexController_client_car.php');

i receive parse error:
"Parse error: syntax error, unexpected T_REQUIRE_ONCE, expecting
T_FUNCTION in
/mnt/hda5/www/zend/dg/car/application/controllers/IndexController.php on
line 6"

When i move require_once outside class definition everything works fine.
But i need require inside
of class because in thouse files are functions which are the member of
that class.
How can i do it ?

Thanx
They're executable statements, so they need to be in a function (or
global). And you can't put them in the constructor as Henrik indicated
or you would be including them in the constructor itself.

But if they're members of the class, I would suggest you just keep them
all in one file. That's what I do. After all - you're going to include
them anyway. Better to include from one file than 10.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 12 '07 #3
avlee wrote:
I have a big class in one php file.
I would like to divide it to many smaller files.
This cannot be done. It's a side-effect of the fact that you cannot
include the construct "?>...<?php" anywhere within a class definition. I
have seen it somewhere in the PHP manual, but can't find it this afternoon!

If you really want to spread your class methods over several files, you
can use inheritance and abstraction. e.g.

A.php:
<?php
abstract class A
{
public function foo() {}
public function bar() {}
}
?>

B.php:
<?php
require_once 'A.php';
abstract class B extends A
{
public function baz() {}
}
?>

C.php:
<?php
require_once 'B.php';
/*concrete*/ class C extends B
{
public function quux() {}
}
?>

test.php:
<?php
require_once 'C.php';
$c = new C;
$c->foo();
$c->bar()
$c->baz()
$c->quux()
?>

But this seems an awfully silly thing to do. There are many good reasons
to use abstract classes. This is not one of them.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 12 '07 #4

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

Similar topics

8
by: CJM | last post by:
For the majority of my work, I just use plain ASP. On the rare occasion that I am doing something intensive that does require user interaction I might create a COM component (ie where performance...
7
by: Bora | last post by:
I usually find that a number of unrelated classes require the same kind of operations. However, I don't want to duplicate code in multiple places. In Java, I've seen those "Utility Classes",...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
4
by: Chuck Bowling | last post by:
I am using CodeDOM to generate source files. The classes being generated have a const string member. This member is referenced in an abstract base class but declared in the inheriting class. I...
4
by: clintonG | last post by:
Using the process class requires the administrator role. Thus, if I may presume, there are other classes that require role-based dependencies. Does anybody know of documentation listing...
4
by: Peter Aitken | last post by:
It seems that some classes have their own exception handling built in. For example if I try to use FileStream to open a file that does not exist, my Try...Catch block seems to be ignored and...
5
by: lister | last post by:
Hi all, I have a fairly diverse range of data that I want to cache in the session rather than pulling it from the database on every page refresh. The problem is is that it seems that PHP...
20
by: Auddog | last post by:
I'm new to working with classes. I'm pretty much a self taught php programmer that uses php mostly for database entry and listings. I would like to expand my talents and start working with...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.