Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP OOP

Status
Not open for further replies.

JamesGMills

Programmer
Aug 15, 2005
157
GB
HI, I have been programming php for years now but I am moving towards Object Orientated php now. I have a question.

I also program in C#.NET and when setting the variables you have to be very strict. So for example this would not work:

class Navigation
{

var $rows_per_page; // used in pagination
var $number_of_results; // total number of rows

var $data_array; // data from the database

function Navigation ()
{

$this->tablename = 'Sections';
$this->dbname = 'jgmcmsc_cms';

}

It would not work because I am trying to put a value into $this->tablename which has not been declared above.

However it seems to still work in php, is this something that more strict in later version of php?

Cheers

------------------------
 
php is both loosely typed and allows on the fly variable declaration.

however, using this method, you cannot declare the visibility of the variable (public/private/protected). late declared variables are assumed to be public. to me, at least, this reduces the usefulness of an object.

however I note that you are using the var keyword, which is, for backwards-compatibility purposes, assumed to be the same as public, so you would see no difference in the real world.
 
Hi, thanks for your reply. Is there a better way than using 'var'?

It does seem a little odd that, as you say, you can create variables on the fly so, it could be said, the time and effort that goes into writing php objects is party to keep things tidy and in order?

------------------------
 
have a look at the visibility section in the classes & Objects (php5) chapter.

there are three visibility operators: public, private, protected; used to control a method and/or a property's visibility to inherited classes and the global scope.
 
From a PHP website: "In PHP V4, properties had to be declared with the keyword var. This is still legal syntax, but mainly for the sake of backward compatibility. In PHP V5, properties should be declared public, private, or protected."

 
Top marks, thanks very much. Might upgrade the server then, very worried about what might not work on clients sites though! Oh well, we will see...

------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top