Jump to content

Beginner PHP question / Multi-instance Processwire


php
 Share

Recommended Posts

Hello, I've been using PHP in an amateurish way for a little while, never really gotten my head around classes etc, but I wrote one for a basic site. I want to make several instances of a site, and link them all to the same Processwire install.

So I'm loading Processwire like so in index.php:

require('/home/centralsite/public_html/wire/core/ProcessWire.php');
$site = new \ProcessWire\ProcessWire('/home/centralsite/public_html/site/', 'http://example.com');

Works fine

Then I'm loading my class

include "submissions.class.php";
$submissions = new Submissions;

My class does a variety of things

<?php
class Submissions {

    private function fields($id=1015) {
		// load some cms fields
    }

    function insert($data) {
		// foobar
    }

    function validate($value,$type) {
        // foobar
    }

}

These functions (redacted) all work fine but... now lets say I want to use some fields etc from my Processwire install inside my class, how do I do it? Eg, if inside $submissions->insert(), I want to get something from $site->pages() or similar.

I tried putting it as a method at the top of my class, and then loading it with $this->connect(); however, if I try to do that more than once, I get errors for redefining a class. So, I think I need to extend the Processwire class somehow?

Link to comment
Share on other sites

So the reason for uses Classes, is that you want to hide complexity and just expose the method in a clean manner, and also you want to deal with properties internally without side effects as you would when doing procedural type of coding. So if you want to pass any data to your classes, there are multiple ways but the best way is to always pass the data by copy in either constructor like this

 

<?php

class Submisssion{
  private data;
  __construct(data){
    	$this->data=data
  }
}

 

or using a Setter method

<?php 
class Submission {
  private data;
  public function insert($data){
    $this->data = $data;
  }
  
$submission->insert($data);

What this does is pass the data through a method and achieves the same as the constructor, because this way the Submission class can handle manipulation of data without external change outside the class.

Which is why OOP is usually preferred as alot of things are hidden and the methods are made simple so you can use a Class without having to read the internal details. 

  • Like 1
Link to comment
Share on other sites

something like this should work:

<?php

class Submissions {
    
    protected $site = null;   // <= add a handle for the site
  
    public function __construct($site) {
        $this->site = $site;  // <= assign the site to the class handle
    }
  
    function insert($data) {
		$this->site->pages->find(....);  // <= use the site through the class handle
    }
}
<?php
require('/home/centralsite/public_html/wire/core/ProcessWire.php');
$site = new \ProcessWire\ProcessWire('/home/centralsite/public_html/site/', 'http://example.com');

include "submissions.class.php";
$submissions = new Submissions($site); // <= here you pass your pw instance ($site) into your class

 

  • Like 2
Link to comment
Share on other sites

3 hours ago, php said:

Thank you both, this is exactly what I needed. @Sephiroth is your code PHP 7? My IDE didn't like it, but the method worked. I have learned something useful!

Lol I write Java in the office so i make the mistake of fusing Java into PHP. 

Link to comment
Share on other sites

4 hours ago, horst said:

something like this should work:


<?php

class Submissions {
    
    protected $site = null;   // <= add a handle for the site
  
    public function __construct($site) {
        $this->site = $site;  // <= assign the site to the class handle
    }
  
    function insert($data) {
		$this->site->pages->find(....);  // <= use the site through the class handle
    }
}

 

 function insert($data) {
        $this->site->pages->find(....);  // <= use the site through the class handle
    }

 

The problem i find with this, is that now your class knows too much about the site pages, it would be better to simply pass the data, else this would lead to tight coupling, unless you have to create an interface and ensure the contracts are followed. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...