Object Array

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
ziuccio
Forum Newbie
Posts: 11
Joined: Mon Jun 28, 2010 11:24 am

Object Array

Post 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
Last edited by ziuccio on Mon Jun 28, 2010 12:39 pm, edited 2 times in total.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Object Array

Post 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();
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object Array

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ziuccio
Forum Newbie
Posts: 11
Joined: Mon Jun 28, 2010 11:24 am

Re: Object Array

Post by ziuccio »

Sure.
I made a mistake pasting the code in the topic.

It is $obj1->getProperty();

But the error persist.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object Array

Post 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();
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ziuccio
Forum Newbie
Posts: 11
Joined: Mon Jun 28, 2010 11:24 am

Re: Object Array

Post by ziuccio »

Exactly!

But how to fix the problem?

I would like to create an array of object...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object Array

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ziuccio
Forum Newbie
Posts: 11
Joined: Mon Jun 28, 2010 11:24 am

Re: Object Array

Post 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.
User avatar
Crys
Forum Newbie
Posts: 4
Joined: Mon Jun 28, 2010 2:23 pm
Location: Belarus, Minsk

Re: Object Array

Post by Crys »

Code: Select all

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

Code: Select all

public function setAllDocuments($val) {
                $this->allDocuments = $val;
}
perfectly
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object Array

Post 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();
                        }
?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object Array

Post 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();
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply