Page 1 of 1

OOP problem

Posted: Fri Jul 07, 2006 1:08 pm
by barry_normal
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello there, can anyone tell me why if I have a class in a separate file like this:

Code: Select all

<?php
class Ob {
var $n = "HELLO WORLD!";
function __construct(){
echo " constructor";
    }
    
    function hiya(){
    echo $n;
    }
}
?>
Then call it like this:

Code: Select all

<?php 
include('Ob.php');
$di = new Ob();
$di->hiya();
?>
Why does nothing happen? It doesn't seem to create a new Ob() when I call it...

Any help would be great, I'm baffled!

Cheers
B


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jul 07, 2006 1:11 pm
by feyd
$n in hiya() does not exist. $this->n would however.

Posted: Fri Jul 07, 2006 1:29 pm
by barry_normal
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks feyd. Much appreciated. I don't really understand what's going on here though.

So if I wanted to pass in a parameter to set the output like this:

Code: Select all

<?php 
include('Ob.php');
$di = new Ob();
$di->hiya();
?>

And then handle it in Ob like this:

Code: Select all

<?php
class Ob {
var $n = "HELLO WORLD!";
 function __construct($in){
 $this->n = $in;
    }
    
    function hiya(){
    echo $this->n;
    }
}
?>
Why doesn't that work?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jul 07, 2006 1:43 pm
by feyd
Your class has a required argument for the constructor while your code isn't supplying one.

Please use

Code: Select all

tags for posting php code

Posted: Fri Jul 07, 2006 1:50 pm
by Christopher
This problem has nothing to do with classes. The short answer is that echo prints the contents of the variable. If the variable contains nothing -- it prints nothing.

Posted: Fri Jul 07, 2006 1:53 pm
by barry_normal
Hi feyd,

Code: Select all

<?php 
include 'Ob.php';
echo "abcdefg";
$send = "HELLO THERE PARAMETER!";
$di = new Ob($send);
$di->hiya();
?>
and

Code: Select all

<?php
class Ob {
var $n;
 function __construct($in){
 $this->n = $in;
    }
    
    
    function hiya(){
    echo $this->n;
    }
}
?>
Doesn't work either...

Posted: Fri Jul 07, 2006 1:56 pm
by feyd
Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array(0, false, '', null, 'off');
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), $neg) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), $neg) ? 'Off' : 'On');
$so = (in_array(strtolower(ini_get('short_open_tag')), $neg) ? 'Off' : 'On');
$le = '';
$cli = (php_sapi_name() == 'cli');
$eol = ($cli ? "\n" : "<br />\n");

$gle = get_loaded_extensions();
$rows = array();
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
    $le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . "\n";
}
if ($cli)
{
     $le = $eol . $le;
}
else
{
 $le = '<pre>' . $le . '</pre>';
}

$ec = array(
   'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
   'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
   'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
   'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
   if (($t & $v) == $v)
   {
      $e[] = $n;
      $t ^= $v;
   }
}
$er = $er . ' (' . implode(' | ', $e) . ')';

if (!$cli)
{
  echo '<html><head><title>quick info</title></head><body>' . "\n";
}

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Short Tags: ' . $so . $eol;
echo 'Display Errors: ' . $de . $eol;
echo 'Loaded Extensions:' . $le . $eol;

if (!$cli)
{
  echo '</body></html>' . "\n";
}

?>

Posted: Fri Jul 07, 2006 2:06 pm
by Ollie Saunders
Run the following in a new file and tell us the results please.
Whoa is that one from feyd's secret stash?

Posted: Fri Jul 07, 2006 2:08 pm
by feyd
ole wrote:
Run the following in a new file and tell us the results please.
Whoa is that one from feyd's secret stash?
Something like that.

In reality, it's a Greasemonkey injected button for the moderators. :)

Posted: Fri Jul 07, 2006 2:11 pm
by barry_normal
Hi feyd,

Sorry for being so backward

Code: Select all

PHP Version: 4.3.11
PHP OS: Darwin
Error Reporting: 2047 (E_ALL)
Register Globals: Off
Short Tags: On
Display Errors: Off
Loaded Extensions:
   xslt         xmlrpc       xml          wddx      
   tokenizer    standard     sockets      session   
   posix        pgsql        pdf          pcre      
   overload     odbc         mysql        mssql     
   ming         mime_magic   mhash        mcrypt    
   mcal         mbstring     ldap         imap      
   iconv        gettext      gd           ftp       
   fbsql        exif         domxml       dbx       
   dbase        curl         ctype        zlib      
   openssl      apache

Posted: Fri Jul 07, 2006 2:17 pm
by feyd
The code you have is written for PHP 5 and up. Your server is running PHP 4.3.11. That's why it "doesn't work."

Posted: Fri Jul 07, 2006 2:22 pm
by Ollie Saunders
PHP 4 provides very limited object oriented capabilities, but you can use constructor with the same name as class instead of __construct:

Code: Select all

class Ob {
var $n;
 function Ob($in){
 $this->n = $in;
    }
   
   
    function hiya(){
    echo $this->n;
    }
Which will in do what you want.
Or better yet upgrade your version of PHP.

Posted: Fri Jul 07, 2006 2:28 pm
by barry_normal
Thanks chaps,

You live and learn.
That's very weird though. I distinctly remember installing 5 before starting any of this.

Obviously didn't work for some reason...

All the best
B