•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 392,074 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,135 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 867 | Replies: 2
![]() |
•
•
Join Date: Aug 2006
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I have a class and defined as follows:
[PHP]
class theclass
{ var a;
var b;
function seta(x)
{this->a=x;
}
function setb(x)
{this->b=x;
}
}
global $myclass;
$myclass=new theclass;
[/PHP]
This is saved in a fileone.php
Then I use it in the fisrt script file as
[PHP]
include_once("fileone.php");
global $myclass;
$myclass->seta(7);
includeonce("filethree.php");[/PHP]
this one is saved as filetwo.php.
My question is on using this class in filethree.php and filefour.php as follows:
Suppose that my filethree.php is
[PHP]
global $myclass;
myclass->setb(8);
$Z=$myclass->a+$myclass->b;
[/PHP]
I will get a Fatal error: Call to a member function a on a non-object.
To avoid this error, I would do filefour.php as follows
[PHP]
include_once("fileone.php");
global $myclass;
myclass->setb(b);
$Z=$myclass->a+$myclass->b;[/PHP]
I would avoid a fatal error but would get a less sever error that calling a member value a that has not been set, or something like that althought I know that I set "a" in filetwo.php
Questions:
(1) Since I declared my class as global in all files, I would expect that all script files will see it and I only need to declare it in the first script file that calls it, in this case filetwo.php after which filethree.php would have seen it without having to include-once again.
(2) If I include_once it in filefour.php, does it mean that the member variable set by filetwo.php will be replaced or destroyed?
[PHP]
class theclass
{ var a;
var b;
function seta(x)
{this->a=x;
}
function setb(x)
{this->b=x;
}
}
global $myclass;
$myclass=new theclass;
[/PHP]
This is saved in a fileone.php
Then I use it in the fisrt script file as
[PHP]
include_once("fileone.php");
global $myclass;
$myclass->seta(7);
includeonce("filethree.php");[/PHP]
this one is saved as filetwo.php.
My question is on using this class in filethree.php and filefour.php as follows:
Suppose that my filethree.php is
[PHP]
global $myclass;
myclass->setb(8);
$Z=$myclass->a+$myclass->b;
[/PHP]
I will get a Fatal error: Call to a member function a on a non-object.
To avoid this error, I would do filefour.php as follows
[PHP]
include_once("fileone.php");
global $myclass;
myclass->setb(b);
$Z=$myclass->a+$myclass->b;[/PHP]
I would avoid a fatal error but would get a less sever error that calling a member value a that has not been set, or something like that althought I know that I set "a" in filetwo.php
Questions:
(1) Since I declared my class as global in all files, I would expect that all script files will see it and I only need to declare it in the first script file that calls it, in this case filetwo.php after which filethree.php would have seen it without having to include-once again.
(2) If I include_once it in filefour.php, does it mean that the member variable set by filetwo.php will be replaced or destroyed?
•
•
Join Date: Aug 2006
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
this should be $this
myclass should $myclass
note that variables are preceded by a $
TYes I see it; however, it is a minor typographic error that could have been caused by typing the code in this forum without testing it. In the real program these syntactic errors are not there since they are normally detected on runtime.
This syntactic error does not answer my question, however, which is on how to use classes in programs.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- Setting up a form in PHP (PHP)
- Opinions? javascript/php/etc and programming standards (JavaScript / DHTML / AJAX)
- PHP Classes (PHP)
- PHP, ASP, ColdFusion, what's your fav? (IT Technologies and Trends)
- PHP and MySQL Web Development (PHP)
- Zend PHP Certification (PHP)
- Alright, Computer Science major... take it or not? (IT Careers and Business)
Other Threads in the PHP Forum
- Previous Thread: problems with sessions and login.php
- Next Thread: PHP - how does it work?


Linear Mode