Can I add myNewClass to an existing class?

Asked by jsherk

Just wondering how I can add myNewClass to the existing $this->template class?

For example, I have plugin.config.xml with this:
<classes>
 <class name="myNewClass" alias="MYNEWPLUGIN_myNewClass" plugin="myNewPlugin" rank="last" />
</classes>

And I have /plugins/myNewPlugin/includes/myNewClass.class.php with this:
<?php
class myNewClass extends ??WhatGoesHere??
{
 public function myNewTemplateFunction()
 {
  //do something
 }
}

So I think I need to just extend the class correct? What is the template class name?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
PHPDevShell Edit question
Assignee:
No assignee Edit question
Solved by:
Greg
Solved:
Last query:
Last reply:
Revision history for this message
TitanKing (titan-phpdevshell) said :
#1

Very simple,

Your class should be extended like so:

class myClassName extends PHPDS_dependant
{

}

then you call it with

$this->factory('myClassName');

myClassName has access to all 7 levels of classes :)

Revision history for this message
jsherk (jeff-forerunnertv) said :
#2

Is it possible to add a new class to extend the template class WITHOUT have to call $this->factory()?

Revision history for this message
jsherk (jeff-forerunnertv) said :
#3

To maybe answer my own question as I am trying to figure this all out, I am thinking the answer would be no from the perspective of adding a new plugin.

Is it only possible if the actually core files themselves are modified (in the /plugins/phpdevshell/ folder)?

Revision history for this message
Greg (gregfr) said :
#4

sorry what do you mean by "add myNewClass to the existing $this->template class"? you want to replace the plain template instance with an instance of your own?

Revision history for this message
jsherk (jeff-forerunnertv) said :
#5

@greg: Ok, I think the better way to describe my question then, is how to do this:
$this->template->myNewFunction()

Is it possible to extend the template class and add a new function to it (from the perspective of installing a new plugin) or would the core files themselves need to be modified?

Revision history for this message
Best Greg (gregfr) said :
#6

It's possible but not from a plugin, however it's easy.

Create a file named "index.local.php" at the root level with this content:

class myClass extends PHPDS_template
{
    .....
}

class PHPDS2 extends PHPDS
{
        public function my_template ($lazy = true)
 {
  if (empty($this->template) && $lazy) {
   $this->template = $this->_factory('myClass');
  }
  return $this->template;
 }
}

$PHPDS = new PHPDS2;

$PHPDS->run();

Revision history for this message
Greg (gregfr) said :
#7

What exactly are you trying to acheive?

Revision history for this message
TitanKing (titan-phpdevshell) said :
#8

The question is why would you want to extend $template, you really dont need to when you...

$myclass = $this->factory('MyClass');
$myclass->someFunction();

You hardly ever need to do what you are atempting to do.

Revision history for this message
jsherk (jeff-forerunnertv) said :
#9

Ok, very interesting... so is index.local.php called/looked at everytime somebody comes to the site, which would then essentially automatically call myClass then?

What I am trying to acheive? Good question!
I am still playing with phpds so I am trying to get familiar with how to do things and I also have never used classes before so I am trying to grasp that concept as well. I think what I was trying to do was avoid having to use the factory call ($myNewClass=$this->factory('myNewClass');) everytime I wanted my function available... as you said $lazy=true (thats me)! But I see now from "installing a plugin" perspective that it can't be done.

Thanks

Revision history for this message
TitanKing (titan-phpdevshell) said :
#10

Well, its not really a matter of cant, its a matter of not really needed. Calling your class is a replacement for including it. Your class will have access to all core methods.

Really simple so dont worry about extending core classes :)

Revision history for this message
jsherk (jeff-forerunnertv) said :
#11

@titanking: I think you are right... I just didn't quite understand classes that well, but now I understand much better!

Thanks

Revision history for this message
jsherk (jeff-forerunnertv) said :
#12

Thanks Greg, that solved my question.

Revision history for this message
Greg (gregfr) said :
#13

I think you should read some course about OOP if you don't know it. You're mixing functions and methods (which is understandable since PHP use the same keyword for both).

A function is available everywhere:

function add1($a, $b)
{
    return $a + $b;
}

print add(5, 6);

A function only rely on its parameters. A method makes only sense as an action on a specific object:

class a
{
    protected $a = 0;
    function add2($b) // this is actually a method NOT a function
    {
        return $this->a + $b;
    }
}

add2(6) makes no sense; it should be used like this:

$o = new a();
print $o->add(6);

Revision history for this message
Greg (gregfr) said :
#14

(in my last post it's better if you don't call the class a to avoid confusion, sorry)

Revision history for this message
jsherk (jeff-forerunnertv) said :
#15

I have only programmed with functions, so I am taking a crash course on classes and methods ... just takes a little playing to get used to it. And of course asking questions about how phpds operates is good for the long run... hopefully I will be able to contribute some code down the road to the project.

Thanks