Page 1 of 1

Object Array

Posted: Mon Jun 28, 2010 11:38 am
by ziuccio
Hi there,


I need an help to do something that in java is working well... but in php gives me some problems.

So:
I create a class to define an object.

Code: Select all

class object1{
  var property;
  function getProperty...
  function setProperty...
}
In another class I retrive values from DB

Code: Select all

class showListDocuments {
	var $allDocuments = array ();

	function getDB() {...}
	function listDocuments() {
		$documents = $db->selectQuery($database, "daily_doc", array (), $condition); //
		$allDocuments= showListDocuments::setAllDocuments($documents);
		for($i=0;$i<count($allDocuments); $i++){
				 
				 $obj1  = new ogbject1();
				 $obj1  ->setProperty(,$allDocuments[$i]['dbField']));
				 $allDocuments[$i]= $obj1;
		}
		
	}
	
	function getAllDocuments(){
		return $this->allDocuments;
	}

	public function setAllDocuments($val) {
		$this->allDocuments = $val;
	}
then i html page

Code: Select all

<?php
	include_once("util/classes/showListDocuments.class.php");
	$documents = new showListDocuments();
	$documents->listDocuments();
	$alldocuments = $documents->getAllDocuments();
?>
<body>
			<?php

			for($i=0; $i<count($alldocuments);$i++){
			        $obj1 = new object1();
				$obj1= $alldocuments[$i];
                                 echo $obj1->getProperty();
			}
			?>
</body>
in the row.

Code: Select all

 $obj1= $alldocuments[$i];
It seems that obj1 lose the the identity of an object and so I can`t use the get method.
Fatal error: Call to a member function getProprieta() on a non-object in C:\xampp\htdocs\project\file.php on line 49
can you help me?
Thanks

Re: Object Array

Posted: Mon Jun 28, 2010 11:59 am
by buckit
I am still learning PHP and dont deal with classes very much at all...

but shouldnt it be

Code: Select all

echo $obj1->getProperty();
instead of

Code: Select all

echo obj1->getProperty();

Re: Object Array

Posted: Mon Jun 28, 2010 12:32 pm
by AbraCadaver
Plus there is at least one parse error and one fatal error in your code. So this must not be the actual code.

Re: Object Array

Posted: Mon Jun 28, 2010 12:38 pm
by ziuccio
Sure.
I made a mistake pasting the code in the topic.

It is $obj1->getProperty();

But the error persist.

Re: Object Array

Posted: Mon Jun 28, 2010 12:46 pm
by AbraCadaver

Code: Select all

//$obj1 is an instance of class object1
$obj1 = new object1();

//$obj1 is now whatever is in $alldocuments[$i] which i assume is maybe a string
$obj1= $alldocuments[$i];

//the non object $obj1 has no methods because it is now a string
echo $obj1->getProperty();

Re: Object Array

Posted: Mon Jun 28, 2010 1:06 pm
by ziuccio
Exactly!

But how to fix the problem?

I would like to create an array of object...

Re: Object Array

Posted: Mon Jun 28, 2010 1:24 pm
by AbraCadaver
ziuccio wrote:Exactly!

But how to fix the problem?

I would like to create an array of object...
I have no idea from your code what you're trying to do, and no offense, but the code makes no sense really. But, to build an array of objects is easy:

Code: Select all

for($i=0; $i<count($alldocuments);$i++){
   $obj1 = new object1();
   $objectArray[] = $obj1;
}

Re: Object Array

Posted: Mon Jun 28, 2010 2:22 pm
by ziuccio
AbraCadaver wrote: I have no idea from your code what you're trying to do, and no offense, but the code makes no sense really. But, to build an array of objects is easy:
in my previous experience in java I used to split view code to buisness code.
In the same situation the steps are the follow in java:

create a class of the object (business object)
create classes to to perform queries and retrive data from the database
Perform classes with method to set the business object with the data retrivied.
print the properties business object in the jsp page with get methods.

I would like to do the same thing in php.
In the view page I would like to print the object properties with get methods. I don`t like getting data directly from the db.
That`s why the code.

Re: Object Array

Posted: Mon Jun 28, 2010 2:43 pm
by Crys

Code: Select all

                $allDocuments= showListDocuments::setAllDocuments($documents);

Code: Select all

public function setAllDocuments($val) {
                $this->allDocuments = $val;
}
perfectly

Re: Object Array

Posted: Mon Jun 28, 2010 2:47 pm
by AbraCadaver
You have already built an array of objects in the listDocuments() method and set it as allDocuments, so why do it again in the view code? Is this what you mean:

Code: Select all

<?php
        include_once("util/classes/showListDocuments.class.php");
        $documents = new showListDocuments();
        $documents->listDocuments();
        $alldocuments = $documents->getAllDocuments();
?>
<body>
                        <?php

                        foreach($alldocuments as $obj){
                                echo $obj->getProperty();
                        }
?>

Re: Object Array

Posted: Mon Jun 28, 2010 2:51 pm
by AbraCadaver
Also, this:

Code: Select all

$allDocuments= showListDocuments::setAllDocuments($documents);
Is a static method call, but the method is operating on $this which is the object instance. Also, it's expecting a return but the method doesn't return anything. Maybe this:

Code: Select all

$this->setAllDocuments($documents);
$allDocuments = $this->getAllDocuments();