Jump to content

[SOLVED] using classes


otuatail

Recommended Posts

Hi. I have never used classes in php before. I tried an example from a website that worked. however I added a second one of mine and it dosn't work. Any ideas?

 

<?php
class MyClass
{
    var $email;
    // use a function without variables
    function check_email(){
        if(ereg("^.+@.+\..+$", $this->email))
            return (true);
        else
            return (false);
    }
    // Use a function with variables
    function image_strip($somehtml){
        $somehtml = preg_replace("/(<img)(.*?)(>)/si", "", $somehtml);
        return $somehtml;
    }
}

class MyCube
{
  //
  var $value;
  var $result;
  
  function calculate()
  {
    $result = $value * $value * $value;
    return $result;
  }
}

?>

 

 

<?php
include "clsMyClass.php";

$myclass = &New MyClass;
$myclass->email = "[email protected]";

$check_email = $myclass->check_email();
if(!$check_email){
    echo "The email address is not valid!";
} else {
    echo "The email address is valid!";
}


$MyCalc = &New MyCube;
$MyCalc ->value = 4;
$Return = calculate();
echo "Value = " . $Return;

?>

 

Thanks. :confused:

Link to comment
https://forums.phpfreaks.com/topic/169914-solved-using-classes/
Share on other sites

Also in your class

 

class MyCube
{
  //
  var $value;
  var $result;
  
  function calculate()
  {
    $result = $value * $value * $value;
    return $result;
  }
}

 

 

should be

 

class MyCube
{
  //
  var $value;
  var $result;
  
  function calculate()
  {
    $result = $this->value * $this->value * $this->value;
    return $result;
  }
}

Link to comment
https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896390
Share on other sites

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.