I am trying to get a handle of OOP using PHP but I am currently stuck and would like some secondary eyes on this. Basically, I am creating an html background randomization script, I have all my objects ready but getting them to work together using an Ajax call is a different story.

When the success method is called back, it only returns 12. What's wrong with RandomImage.php that is causing it too break?

Step 1. Setup my Ajax call using jQuery

$(document).ready(function(){
			$.ajax({ 
				type: "POST",
				url: "scripts/getRandomTile.php",
				data: "path=http://localhost:8888/otc.ca/media/images/patterns",
				success: function(data){
					$("body").append("<p>data: " + data + "</p>");
		      	}
	      	});
		});

getRandtomTile.php

include_once("http://localhost:8888/lib/classes/php/utils/web/html/RandomImage.php");

echo "1";
echo "2";

$img = new RandomImage();

echo "3";

RandomImage.php

include_once("http://localhost:8888/lib/classes/php/utils/system/openDir.php");

class RandomImage
{
	var $images;
	var $name = "marlon"; 	

	function getImages ($folder_path)
	{
		$opendir = new openDir();
		$opendir->setpath($folder_path);

		$this->images = $opendir->getfiles();
	}

	function getImage()
	{
		//array_rand($this->images, 1);
		return $this->images[0];
	}
}

    within my getRandomTile.php file, this does executes the else statement

    include "http://localhost:8888/lib/classes/php/utils/web/html/RandomImage.php";
    
    if (class_exists('RandomImage')) {
    	echo "continue";
    } else {
    	echo 'ERROR: class RandomImage does not exist';
    }
    

    ...but changing the include to a relative path gives me the true statement.

    include "../../lib/classes/php/utils/web/html/RandomImage.php";
    

    why is that? the project is here http://localhost:8888/project/ and classes are here http://localhost:8888/lib/

      Ok, it all boiled down too relative paths so I am going to need to read up on this 'cuz me no like. I want to use full paths to be able to re-use them. Any suggestions? Should I pass the class path within my ajax call?

        turn errors on it will tell you

        // initialiaze.inc.php
        
        
        // Define the core paths
        // Define them as absolute paths to make sure that require_once works as expected
        
        // DIRECTORY_SEPARATOR is a PHP pre-defined constant
        // (\ for Windows, / for Unix)
        defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
        
        defined('SITE_ROOT') ? null : define('SITE_ROOT',DS.'var'.DS.'www'.DS.'html'.DS.'kodak');
        
        defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
        

          You can't use absolute paths in includes. Otherwise you would be able to include someone elses website scripts and that would be a huge security risk. Make sure you only try to use relative paths

            zypher11;10954194 wrote:

            You can't use absolute paths in includes.

            That's entirely false. Where on earth did you get that idea?

            zypher11;10954194 wrote:

            Otherwise you would be able to include someone elses website scripts and that would be a huge security risk.

            Welcome to the insecure world of shared hosting. 🙂

              Ok let me rephrase that last post. Most websites, by default, disallow URL file access to prevent you from including their config files and connecting to their database. So technically you can call an external file via absolute path but the website needs to allow it first.

                Woops, I didn't scroll up to read the OP's first post.

                @: As zypher11 points out, accessing files via a URL (e.g. any path that begins with "http(s)://") is very different than accessing them via the file system. You're attempting to include() a PHP script via a URL, which is only going to give you the output of that script - not the script itself. Why? Because including via URL would be the same thing as opening your browser, typing in the URL, viewing the source of the page, and using that data.

                Including via the file system (e.g. a local path, such as one that starts out as '/lib/classes/') means PHP will actually include the file itself rather than making an HTTP request to the webserver.

                  thanks for the feedback, i'm coming from the actionscript world and I was looking at how i could keep all my classes in a separate folder from my php project. so the best solution is to always copy my lib folder into each project/site/host.

                  thanks.

                    sophistikat wrote:

                    so the best solution is to always copy my lib folder into each project/site/host.

                    And have duplicate files all over the place?

                    If this "lib" folder contains static classes that you've created, then you should keep one copy of them in a central location. You could add this location to your [man]include_path[/man] for easier inclusion if you so desire.

                      ok, so in my local files i have these folders:
                      /lib/php
                      /project1
                      /project2

                      using the include_path, i can define the central location of all my php classes

                      what's the best practice when going live? for my personal website i was thinking of having a subdomain with all my classes:

                      lib.host.com/php
                      www.host.com

                      how about when doing a project for a client, i imagine i'd copy my php library folder for them to use?

                      thanks for the help, i am trying to see what's the best practice in PHP. with actionscript is a lot easier because your objects get compiled with the swf file.

                        For PHP libraries (e.g. collections of classes/etc. you want to re-use), there's no reason they should/need to be web-accessible.

                        sophistikat wrote:

                        with actionscript is a lot easier because your objects get compiled with the swf file.

                        Well with PHP, there is no compilation on your part - the PHP interpreter does the parsing, compiling, and executing all at runtime. You could mimic the same behavior, however, by simply pasting all of the classes into the same PHP file.

                        That would, however, defeat one of the benefits of using generic OOP libraries in that code should be design so that it's reusable/modularized.

                          Write a Reply...