OOP problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

OOP problem

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$n in hiya() does not exist. $this->n would however.
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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";
}

?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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."
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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
Post Reply