User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2006
Posts: 6
Reputation: kibanga is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kibanga kibanga is offline Offline
Newbie Poster

Classes In Php

  #1  
Aug 28th, 2006
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 166
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

Re: Classes In Php

  #2  
Sep 1st, 2006
this should be $this
myclass should $myclass

note that variables are preceded by a $
"Premature optimization is the root of all evil."
Donald Knuth / Tony Hoare
Reply With Quote  
Join Date: Aug 2006
Posts: 6
Reputation: kibanga is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kibanga kibanga is offline Offline
Newbie Poster

Re: Classes In Php

  #3  
Sep 1st, 2006
Originally Posted by pritaeas View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC