Jump to content

[SOLVED] How do some programmers do this...


Demonic

Recommended Posts

PHP 5 stuff, it wont work correctly with PHP 4.

 

How would I do this just using PHP 4?

 

I got an error with your example:

 

Parse error: syntax error, unexpected T_VAR, expecting T_VARIABLE in C:\SERVER\www\foobar.php on line 4

 

 

I'm using WAMP 5 PHP 5.2.3

I suppose that it isn't necessary to declare a function public, since according to the manual a method without a declaration is automatically public (http://us2.php.net/manual/en/language.oop5.visibility.php#language.oop5.visiblity-methods).

 

I do it mostly for consistency, and also because I suppose that I'm slightly OCD.

 

Remove the public / protected from the vars and methods, and change the function named "__construnct" to the name "foo".

I suppose that it isn't necessary to declare a function public, since according to the manual a method without a declaration is automatically public (http://us2.php.net/manual/en/language.oop5.visibility.php#language.oop5.visiblity-methods).

 

I do it mostly for consistency, and also because I suppose that I'm slightly OCD.

 

Remove the public / protected from the vars and methods, and change the function named "__construnct" to the name "foo".

 

Ok cool it worked perfectly:

 

<?php

class foo {
var $bar;

function foo () {
	$this->bar = new bar();
}
}

class bar {
function foo_bar() {
	return "FUBAR (F'd Up Beyond All Repair)";
}
}

$blah = new foo;

echo $blah->bar->foo_bar();

?>

IMHO, specifying things which are the default anyway (such as public) is good practice.  It adds to the documentation, and ensures your code will run correctly even if the defaults change.  It also helps catch bugs, as you may not always be correct in what you assume the default to be.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.