Welcome Guest, Not a member yet? Register   Sign In
Passing Objects As Function Parameters
#1

[eluser]Shouvik-S-Mazumdar[/eluser]
Ok i am from C# background and while coding in Codeigniter can i use the oops to pass param..

like suppose i have

function write_blog($title,$body,$date)
{

// some implementations

}


instead of that

class Blog
{

public string title;
public string body;
public string date;


}



// re writing the write method

function write_blog(Blog b)
{

// some implementations

}



Similarly if i want to read Blogs do i have a collection object like

List<Blog>



====

am sorry of this is a basic PHP function... do help me out ..would be gr8ful

thnx
#2

[eluser]imn.codeartist[/eluser]
[quote author="Shouvik-S-Mazumdar" date="1262817076"]Ok i am from C# background and while coding in Codeigniter can i use the oops to pass param..

like suppose i have

function write_blog($title,$body,$date)
{

// some implementations

}


instead of that

class Blog
{

public string title;
public string body;
public string date;


}



// re writing the write method

function write_blog(Blog b)
{

// some implementations

}



Similarly if i want to read Blogs do i have a collection object like

List<Blog>



====

am sorry of this is a basic PHP function... do help me out ..would be gr8ful

thnx[/quote]

This code will help your problem


Code:
class Blog
{

   var $title;
   var $body;
   var $date;


}

function write_blog($blog)
{

   echo $blog->title ."<br/>";
   echo $blog->body."<br/>";
   echo $blog->date."<br/>";

}

Implementation

$blog=new Blog();
$blog->title="this is title";
$blog->body="this is Body";
$blog->date=date('Y-m-d');

write_blog($blog);

Since PHP has not generic collection type

you can store into arrays

for example
Code:
$blog_coll=array($blog1,$blog2);
or

$blog_coll[0]=$blog1;
$blog_coll[0]=$blog2;
#3

[eluser]danmontgomery[/eluser]
You can also do simple error checking to make sure you're getting the correct object type

Code:
if(!is_a($blog, "Blog")){
  // Some error...
}
#4

[eluser]Shouvik-S-Mazumdar[/eluser]
I created the Blog class in Model Folder. I then use this as said

$bloglist = array();
foreach($query->result() as $row)
{

$blog = new $blog(); // THROWS AN ERROR HERE
$blog->blog_id = $row->id;
$blog->blog_title = $row->title;
$blog->blog_body = $row->body;
$blog->blog_category = $row->category;
$blog->blog_status = $row->status;

// add it to the generic array
$bloglist[] = $blog;
}

return $bloglist;


============

ERROR :

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: blog

Filename: models/blogmodel.php

Line Number: 108
#5

[eluser]danmontgomery[/eluser]
Code:
$blog = new $blog();  // THROWS AN ERROR HERE
should be
Code:
$blog = new blog();  // THROWS AN ERROR HERE

assuming your class is named 'blog'
#6

[eluser]Shouvik-S-Mazumdar[/eluser]
nopes man ... when i do it i get a runtime exception 9 the page appers blank as PHP global error reporting is off )

am i missing anything ???

foreach($query->result() as $row)
{

$blog = new Blog(); // IF I REMOVE THIS LINE THE NEXT SENTENCE EXECUTES OTHERWISE NOT
echo "hi";



}
#7

[eluser]Shouvik-S-Mazumdar[/eluser]
THIS IS MY CLASS

////////////////////////////////////////////

&lt;?php

class Blog
{
/* Contains the post in the blog */
/* members */

public function Blog()
{

}

var $blog_id;
var $blog_title;
var $blog_body;
var $blog_summary;
var $blog_timestamp;
var $blog_category;
var $blog_status;



}


?&gt;
#8

[eluser]danmontgomery[/eluser]
Your code works for me, are you including the file where the class is defined?

You should also be able to put the code in a try/catch block to get the runtime exception error

Code:
try {
  // Some code here
} catch(RuntimeException $e) {
  echo "Error" . $e->getMessage();
}
#9

[eluser]Shouvik-S-Mazumdar[/eluser]
nopes... Sad oops forgot that

actually am using an IDE which is able to recognize the class so i assumed you might not need to add a reference to the class.

anyways how to do that
#10

[eluser]Shouvik-S-Mazumdar[/eluser]
How to include a class ? should i do it the usual PHP way ? like include "blog.php";


just a doubt ... every time i need it should i include it or is der any way i can restrict the number of instances as one - aka static class in c#




Theme © iAndrew 2016 - Forum software by © MyBB