Jump to content

Recommended Posts

Hello World,

I have what may seem like a dumb question. Maybe what I am trying to do should be done differently. This question is both a "best practice" and "help with code." I am very new to OOP so please excuse the ignorance. Within the code examples below you will find other questions.

 

Lets pretend I have a page that I want to create a new "Roll" class. The Roll class has a function to scan folder for all files of type jpeg. I need instances for each thing returned in this scan be associated with a new type Picture.

 

Should picture extend class roll????

 

class Roll{

private $roll_id;

private $roll_date;

private $roll_pict_array = array();

 

 

// special __functions //

public function __constructor(){

//////////////////////////QUESTION 1

// It seems like I should use the constructor for something but I cant find a reason for it.

//I know that anything with in the constructor gets executed when an instance is created but i cant seem to fathom a practical use. books haven't helped in this department

 

}

public function scanPictureFolder(){

$dir_to_read = "/Users/quijote/Sites/_tempdata/";

 

while (false !== ($file = readdir($handle))) {

if($file !="." && $file !=".." && preg_match("/\.jpg$|\.jpeg$/i", $file)){

array_push($this->roll_pict_array, $dir_to_read.$file);

 

//////////////////////////QUESTION 2

// Instead of doing the array_push and then using the createPictureInstance below I should just create an instance here of picture here for each thing returned. I cant seem to figure which is the best practice.

}

}

    }

 

    public function createPictureInstance(){

//$numpicts = count($this->roll_pict_array);

//if($i=0;$i<$numpicts;$i++){

foreach($this->roll_pict_array as $element){

//echo($element);

 

}

}

}

I would like to create a new instance of Picture for each file found. I would like these Pictures to be associated with Roll.

 

Link to comment
https://forums.phpfreaks.com/topic/53234-instanciation-best-practice-sub-class/
Share on other sites

If what you meant is you would like to do is fill an array in Roll with a new picture instance you could do something like this...

 

<?php
/**
* This probably doesn't match your intent (since Roll made no sense to me I changed the class name)
* This is just an example to help explain what I think you're saying is your problem
* I also used constructors to show you a use for them
*/
class Dir
{
private $directory;
private $contents;

// Set the directory
// We use a construct since a "Directory" class should never be without a directory set
public function __construct($directory)
{
	$this->directory = $directory;
	$this->updateContents();
}

// Update the contents of current directory
public function updateContents()
{
	$contents = scandir($this->directory);
	foreach($contents as $item)
	{
		if($item != '.' && $item != '..')
		{
			$this->contents[] = $item;
		}
	}
}

// Search the directory for all JPEG images
public function getPictures()
{
	$pictures = array(); // Set an array to put the pictures in
	foreach($this->contents as $fileName)
	{
		if(preg_match("/\.jpg$|\.jpeg$/i", $fileName))
		{
			$pictures[] = new Picture($this->directory, $fileName);
		}
	}
	return $pictures;

}
}

class Picture
{
private $directory;
private $fileName;

// Set the directory and the file name
// We use a construct since a "Picture" should never be without a location
public function __construct($directory, $fileName)
{
	$this->directory = $directory;
	$this->fileName = $fileName;
}

// Maybe a function to make use of the class
public function getLocation()
{
	return $this->directory . '/' . $this->fileName;
}
}

// Test the class out...
$directory = new Dir('C:\Documents and Settings\Matthew\Desktop\Files\Misc\Other Images');
$pictures = $directory->getPictures();

// Get the first image location... (if it exists)
if(isset($pictures[0]))
{
echo $pictures[0]->getLocation();
}
else {
echo 'There is no $picture[0]';
}
?>

 

You normally shouldn't "extend" a class unless you mean to make a more specialized version of a class...

For example...

Animal -> Feline -> House Cat

 

or a more in-the-wild example...

Controller -> ApplicationController -> NewsController

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.