Jump to content

Recommended Posts

Okay I'm trying to get a grasp on OOP

 

The book I am reading is "PHP and MySQL Web Development" Third Edition.

 

Here's what i got so far..

<?php include("class_lib.php");

$a = new classname('First');
$b = new classname('Second');
$c = new classname('Third');

?>

 

 

class_lib.php

 
<?php

class classname
{

function __construct($param){

   echo "Constructor called with parameter $param <br />" ;
   
   }
   
}
   

 

 

But, nothing is being outputted.

 

What am I doing wrong? =( 

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/
Share on other sites

as it stands now.. it seems like OOP will take a little while to fully understand.. =/

 

but i wont give up.

 

PHP4 is bad to try programming in OOP in, I would try and do it on a PHP 5 server. Since PHP4 OOP will be outdated once most servers are 5, it is better to code for the future, not the past. Plus PHP5 handles OOP a ton better than 4 does.

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/#findComment-710707
Share on other sites

as it stands now.. it seems like OOP will take a little while to fully understand.. =/

 

but i wont give up.

 

Not to worry you, but it took me 7 years just to understand why it might be useful :P

 

As premiso said, move to PHP5 asap. OOP in PHP4 is not OOP. It just mimicks some OOP-like behaviours.

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/#findComment-710711
Share on other sites

Okay i upgraded to PHP5 :)

 

I'm starting to "Kind of" understand OOP.

 

Here is something i wrote,

 

<?php

class classname

{
var $total;
function __construct($x,$y) {

  
  $this->total = $x*$y;
  echo $x." times ".$y." equals ".$this->total."<br />";
  }
  
}


$a = new classname('5','2');
$b = new classname('10','3');

?>

 

 

I know its pretty beginner stuff.. but im excited lol

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/#findComment-710733
Share on other sites

A few suggestions...  I'm not a pro PHP OOP guy, so take it with a grain of salt, but this is what I have learned.

class classname
{
private $total;  // using var is outdated (php4 style) look in to public, private, etc. variables
public $var1;
public $var2;

function __construct($x,$y) {
  $this->total = $x*$y;
  $this->var1 = $x;
  $this->var2 = $y;
}
public function getTotal(){
//you should do the same thing with declaring methods, use public, private, static etc.
//its also not a good idea to echo from a class, you should return the data so you can format it in your code and keep a separation of HTML and Data.
   return $this->total;
}
}
$a = new classname('5','2');
$b = new classname('10','3');

//now you have more control on how you format your data via html
echo $a->var1 . ' times ' . $a->var2 . ' equals ' . $a->getTotal() . '<br />';
echo $b->var1 . ' x ' . $b->var2 . ' = ' . $b->getTotal() . '<br />';

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/#findComment-710746
Share on other sites

okay i took your advice and heres what i came up with

 

class_lib.php


<?php
class process {

public $x;
public $y;


    public function add(){
    
       return $this->x + $this->y;
       
       }
    public function subtract(){
    
       return $this->x - $this->y;
    
       }
       
    public function multiply(){
    
    return $this->x * $this->y;
    
    
    
       }
       
    public function divide(){
    
    return $this->x / $this->y;
    

       }
       
    
       


}
?>

 

 

index.php

<?php
                                   
include("class_lib.php");




$add = new process();

$add->x = 10;
$add->y = 11;

echo $add->x." minus ".$add->y." equals ".$add->subtract()."<br />";
echo $add->x." times ".$add-y." equals ".$add->multiply()."<br />";
echo $add->x." plus ".$add-y." equals ".$add->add()."<br />";
echo $add->x." divided by ".$add-y." equals ".$add->divide()."<br />";



?>

 

This gives me an eror of :

 

Catchable fatal error: Object of class process could not be converted to string in /homepages/29/d119570661/htdocs/wmptest.com/ooptraining/index.php on line 14

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136240-oop/#findComment-710796
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.