views:

64

answers:

6

I have a class called myClass in /myDir/myClass.php. When user types url:

http://mysite.com/myDir/myClass.php,  

I want to automatically create an instance of myClass. What technique can I use to do this? the idea is to use myDir directory as top level of programs that user can call directly, but I don't want to add instance_of_myClass = new myClass(); because then I won't be able to extend the class. Does this make sense?

class myClass
{    
  __construct()
  {
     echo "hello World";
     $this->myClass_report();
  }
  myClass_report()
  {
      // some code here
  }


}   
A: 
class myClass
{    
  __construct()
  {
     echo "hello World";
     myClass_report();
  }
  myClass_report()
  {
      // some code here
  }


} 

$x = new myClass();
From.ME.to.YOU
...but I don't want to add instance_of_myClass = new myClass(); because then I won't be able to extend the class
bob
A: 

why do you want to do this?

amhed
I want to make a cleaner 'framework' where controller classes are automatically instanced when user points to them. I am not sure if this is good idea, but I realized that defining a class and creating an instance of the class in the same file may not be a good idea.
bob
A: 

If you are not creating an instance with

$classINstance = new myClass();

Then you don't have a class instance. That has nothing to do with extending a class.

You extend a class (i assume you mean inheritance?), then you create a new class that extends a parent class. To use this class you also create a new instance with the new operator.

At least if you are not using the static keyword to create static methods for a class.

I had a quick search, maybe have a look at this questions accepted answer. Lookd like a good summary that could help you:

OOP

Or i just didn't understand what you meant ;)

TheCandyMan666
fascinating thread! http://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me
bob
+3  A: 

.htaccess:

#if You have access, omit RewriteBase and put the rule in http.conf,
#with a / in the beginning of the pattern.
RewriteBase /
RewriteCond $0 !=index.php
RewriteRule .* index.php?path=$0 [QSA,B]

index.php

//check if the file exists and it's allowed
if (!check_if_allowed_path(realpath($_GET['path']))) {
    //access denied; check e.g. against the document root or
    //something more complex
}
//or use autoload instead
include $_GET['path'];
$classname = basename($_GET['path'], '.php');
$instance = new $classname();
Artefacto
this sounds like what I what I am looking for, but will my post variables fall off?
bob
@bob The POST content will be untouched by this.
Artefacto
$classname = str_replace(".php", "", basename($_GET['path']));
bob
@bob Fixed. (more chars)
Artefacto
A: 

I don't understand what you're trying to do, but maybe this will help:

If you want to create an instance of myClass, but don't want the new myClass to be hard-coded everywhere, you could use a factory (wikipedia) of some kind.

grossvogel
this is what I am looking for
bob
A: 

Use the factory design pattern:

<?php
//Factory.php
include_once('Box.php');

class My_Factory
{
  private $this->outcome;

  public function __construct()
  {
    this->outcome = $this->doSomeWeirdShitToSetupTheFactoryAndBlueprintSomething();
  }

  private function doSomeWeirdShitToSetupTheFactoryAndBlueprintSomething()
  {
    //1+1
  }

  public function giveMeAnInstanceOfOutcome()
  {
    return new My_Box($this->outcome);
  }
}

Edit: I read it again and I have one question: Why do you want a user to type http://yourl/YourClass.php ???

Classes should be included; never ever ever used to load directly to the browser.

baklap
can I combine this idea with a factory pattern?
bob
"never ever ever used to load directly" - you seem to know what you are talking about, but I am bot sure why? I am trying to move to pure OO paradigm, so I said OK, I will make all top level controllers as instances of a class.
bob
Because your class is your blueprint. If you create a new invention you'd rather sell the product than the blueprint. All your classes should be instantiated in one file: your index.php. You bootstrap some prework and then classes do the work and even output your page.
baklap
OK. this is what I am doing now in index.php
bob